Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix coding standard issues #172

Merged
merged 1 commit into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 35 additions & 31 deletions src/Visitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ class Visitor extends NodeVisitor
/** @var ?array<string,array<int|string,string>> */
private $functionMap = null;

/** @var array<string, array<int, \PhpStubs\WordPress\Core\WordPressTag>> */
/** @var array<string, list<\PhpStubs\WordPress\Core\WordPressTag>> */
private $additionalTags = [];

/** @var array<string, array<int, string>> */
/** @var array<string, list<string>> */
private $additionalTagStrings = [];

private \PhpParser\NodeFinder $nodeFinder;
Expand All @@ -50,7 +50,9 @@ public function __construct()
$this->nodeFinder = new NodeFinder();
}

/** @return int|null */
/**
* @return ?int
*/
public function enterNode(Node $node)
{
$voidOrNever = $this->voidOrNever($node);
Expand Down Expand Up @@ -190,7 +192,7 @@ private function postProcessNode(Node $node): void
}

/**
* @return array<int, \PhpStubs\WordPress\Core\WordPressTag>
* @return list<\PhpStubs\WordPress\Core\WordPressTag>
*/
private function generateAdditionalTagsFromDoc(Doc $docComment): array
{
Expand All @@ -204,20 +206,20 @@ private function generateAdditionalTagsFromDoc(Doc $docComment): array
return [];
}

/** @var list<\phpDocumentor\Reflection\DocBlock\Tags\Param> $params*/
$params = $docblock->getTagsByName('param');
/** @var list<\phpDocumentor\Reflection\DocBlock\Tags\Param> $paramTags*/
$paramTags = $docblock->getTagsByName('param');

/** @var list<\phpDocumentor\Reflection\DocBlock\Tags\Return_> $returns */
$returns = $docblock->getTagsByName('return');
/** @var list<\phpDocumentor\Reflection\DocBlock\Tags\Return_> $returnTags */
$returnTags = $docblock->getTagsByName('return');

/** @var list<\phpDocumentor\Reflection\DocBlock\Tags\Var_> $vars */
$vars = $docblock->getTagsByName('var');
/** @var list<\phpDocumentor\Reflection\DocBlock\Tags\Var_> $varTags */
$varTags = $docblock->getTagsByName('var');

/** @var list<\PhpStubs\WordPress\Core\WordPressTag> $additions */
$additions = [];

foreach ($params as $param) {
$addition = self::getAdditionFromParam($param);
foreach ($paramTags as $paramTag) {
$addition = self::getAdditionFromParam($paramTag);

if (! ($addition instanceof WordPressTag)) {
continue;
Expand All @@ -226,8 +228,8 @@ private function generateAdditionalTagsFromDoc(Doc $docComment): array
$additions[] = $addition;
}

foreach ($returns as $return) {
$addition = self::getAdditionFromReturn($return);
foreach ($returnTags as $returnTag) {
$addition = self::getAdditionFromReturn($returnTag);

if (! ($addition instanceof WordPressTag)) {
continue;
Expand All @@ -236,8 +238,8 @@ private function generateAdditionalTagsFromDoc(Doc $docComment): array
$additions[] = $addition;
}

foreach ($vars as $var) {
$addition = self::getAdditionFromVar($var);
foreach ($varTags as $varTag) {
$addition = self::getAdditionFromVar($varTag);

if (! ($addition instanceof WordPressTag)) {
continue;
Expand Down Expand Up @@ -273,7 +275,7 @@ static function (WordPressTag $tag): string {
return '';
}

return ' * ' . implode("\n * ", $lines);
return sprintf(' * %s', implode("\n * ", $lines));
},
$additions
);
Expand All @@ -294,8 +296,8 @@ static function (WordPressTag $tag): string {
}

/**
* @param array<int, \PhpStubs\WordPress\Core\WordPressTag> $additions
* @return array<int, \PhpStubs\WordPress\Core\WordPressTag>
* @param list<\PhpStubs\WordPress\Core\WordPressTag> $additions
* @return list<\PhpStubs\WordPress\Core\WordPressTag>
*/
private function discoverInheritedArgs(DocBlock $docblock, array $additions): array
{
Expand Down Expand Up @@ -334,7 +336,7 @@ static function (WordPressTag $addition): bool {
}

/**
* @return array<int, \PhpStubs\WordPress\Core\WordPressTag>
* @return list<\PhpStubs\WordPress\Core\WordPressTag>
*/
private function getInheritedTagsForParam(Param $param): array
{
Expand All @@ -356,7 +358,7 @@ private function getInheritedTagsForParam(Param $param): array
return [];
}

list($description) = explode("\n\n", $paramDescription->__toString());
$description = explode("\n\n", $paramDescription->__toString())[0];

if (strpos($description, '()') === false) {
return [];
Expand Down Expand Up @@ -389,7 +391,7 @@ private function getInheritedTagsForParam(Param $param): array
}

/**
* @param array<int, \PhpStubs\WordPress\Core\WordPressTag> $tags
* @param list<\PhpStubs\WordPress\Core\WordPressTag> $tags
*/
private static function getMatchingInheritedTag(Param $param, array $tags, string $symbolName): ?WordPressTag
{
Expand Down Expand Up @@ -427,7 +429,7 @@ static function (WordPressTag $tag) use ($matchNames): bool {
private function getAdditionalTagsFromMap(string $symbolName): array
{
if (! isset($this->functionMap)) {
$this->functionMap = require dirname(__DIR__) . '/functionMap.php';
$this->functionMap = require sprintf('%s/functionMap.php', dirname(__DIR__));
}

if (! isset($this->functionMap[$symbolName])) {
Expand Down Expand Up @@ -636,17 +638,18 @@ private static function getTypeNameFromDescriptionString(?string $tagDescription
return null;
}

list(, $items, $final) = $matches;
$items = $matches[1];
$final = $matches[2];

// Pluck out phrases between single quotes, so messy sentences are handled:
preg_match_all("#'([^']+)'#", $items, $matches);

list(,$accepted) = $matches;
$accepted = $matches[1];

// Append the final item:
$accepted[] = $final;

return "'" . implode("'|'", $accepted) . "'";
return sprintf("'%s'", implode("'|'", $accepted));
}

private static function getTypeNameFromType(Type $tagVariableType): ?string
Expand Down Expand Up @@ -683,7 +686,7 @@ private static function getTypeNameFromString(string $tagVariable): ?string
continue;
}
// Move the type that uses the hash notation to the end of union types so the shape works.
$tagVariableType = str_replace("{$supportedType}|", '', $tagVariableType) . "|{$supportedType}";
$tagVariableType = sprintf('%s|%s', str_replace("{$supportedType}|", '', $tagVariableType), $supportedType);
}

return $tagVariableType;
Expand Down Expand Up @@ -728,7 +731,8 @@ private static function getTypesAtLevel(string $text, bool $optional, int $level
return [];
}

list($type, $name) = $parts;
$type = $parts[0];
$name = $parts[1];

$optionalArg = $optional;
$nameTrimmed = ltrim($name, '$');
Expand Down Expand Up @@ -789,15 +793,15 @@ private function voidOrNever(Node $node): string
return '';
}

$return = $this->nodeFinder->findInstanceOf($node, Stmt_Return::class);
$returnStmts = $this->nodeFinder->findInstanceOf($node, Stmt_Return::class);

// If there is a return statement, it's not return type never.
if (count($return) !== 0) {
if (count($returnStmts) !== 0) {
// If there is at least one return statement that is not void,
// it's not return type void.
if (
$this->nodeFinder->findFirst(
$return,
$returnStmts,
static function (Node $node): bool {
return isset($node->expr);
}
Expand Down
12 changes: 4 additions & 8 deletions src/WordPressTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,13 @@

final class WordPressTag extends WithChildren
{
/** @var string */
public $tag;
public string $tag;

/** @var string */
public $type;
public string $type;

/** @var ?string */
public $name = null;
public ?string $name = null;

/** @var ?string */
public $description = null;
public ?string $description = null;

/**
* @return list<string>
Expand Down
Loading