diff --git a/phpcs.xml.dist b/phpcs.xml.dist index 1cb3002..b20596d 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -8,6 +8,7 @@ + diff --git a/psalm.xml b/psalm.xml index 7b90153..4b0caa7 100644 --- a/psalm.xml +++ b/psalm.xml @@ -13,13 +13,18 @@ - - + + + + + + + diff --git a/src/DocBlock.php b/src/DocBlock.php index 75848c6..f3403d6 100644 --- a/src/DocBlock.php +++ b/src/DocBlock.php @@ -19,7 +19,7 @@ use Webmozart\Assert\Assert; final class DocBlock { /** @var string The opening line for this docblock. */ - private $summary = ''; + private $summary; /** @var DocBlock\Description The actual description for this docblock. */ private $description; @@ -34,10 +34,10 @@ final class DocBlock private $location; /** @var bool Is this DocBlock (the start of) a template? */ - private $isTemplateStart = false; + private $isTemplateStart; /** @var bool Does this DocBlock signify the end of a DocBlock template? */ - private $isTemplateEnd = false; + private $isTemplateEnd; /** * @param DocBlock\Tag[] $tags diff --git a/src/DocBlock/DescriptionFactory.php b/src/DocBlock/DescriptionFactory.php index 3624a9b..8f94e15 100644 --- a/src/DocBlock/DescriptionFactory.php +++ b/src/DocBlock/DescriptionFactory.php @@ -158,7 +158,7 @@ class DescriptionFactory // determine how many whitespace characters need to be stripped $startingSpaceCount = 9999999; - for ($i = 1; $i < count($lines); ++$i) { + for ($i = 1, $iMax = count($lines); $i < $iMax; ++$i) { // lines with a no length do not count as they are not indented at all if (strlen(trim($lines[$i])) === 0) { continue; @@ -171,7 +171,7 @@ class DescriptionFactory // strip the number of spaces from each line if ($startingSpaceCount > 0) { - for ($i = 1; $i < count($lines); ++$i) { + for ($i = 1, $iMax = count($lines); $i < $iMax; ++$i) { $lines[$i] = substr($lines[$i], $startingSpaceCount); } } diff --git a/src/DocBlock/Serializer.php b/src/DocBlock/Serializer.php index 9db3d52..531970b 100644 --- a/src/DocBlock/Serializer.php +++ b/src/DocBlock/Serializer.php @@ -98,10 +98,7 @@ class Serializer return $comment . $indent . ' */'; } - /** - * @return mixed - */ - private function removeTrailingSpaces(string $indent, string $text) + private function removeTrailingSpaces(string $indent, string $text) : string { return str_replace( sprintf("\n%s * \n", $indent), @@ -110,10 +107,7 @@ class Serializer ); } - /** - * @return mixed - */ - private function addAsterisksForEachLine(string $indent, string $text) + private function addAsterisksForEachLine(string $indent, string $text) : string { return str_replace( "\n", diff --git a/src/DocBlock/StandardTagFactory.php b/src/DocBlock/StandardTagFactory.php index ef8c6fe..0a5568e 100644 --- a/src/DocBlock/StandardTagFactory.php +++ b/src/DocBlock/StandardTagFactory.php @@ -212,6 +212,7 @@ final class StandardTagFactory implements TagFactory try { $callable = [$handlerClassName, 'create']; Assert::isCallable($callable); + /** @phpstan-var callable(string): ?Tag $callable */ $tag = call_user_func_array($callable, $arguments); return $tag ?? InvalidTag::create($body, $name); @@ -222,6 +223,8 @@ final class StandardTagFactory implements TagFactory /** * Determines the Fully Qualified Class Name of the Factory or Tag (containing a Factory Method `create`). + * + * @return class-string */ private function findHandlerClassName(string $tagName, TypeContext $context) : string { diff --git a/src/DocBlock/Tag.php b/src/DocBlock/Tag.php index 647f018..f55de91 100644 --- a/src/DocBlock/Tag.php +++ b/src/DocBlock/Tag.php @@ -21,6 +21,8 @@ interface Tag /** * @return Tag|mixed Class that implements Tag + * + * @phpstan-return ?Tag */ public static function create(string $body); diff --git a/src/DocBlock/Tags/Author.php b/src/DocBlock/Tags/Author.php index 0f759c7..048dd48 100644 --- a/src/DocBlock/Tags/Author.php +++ b/src/DocBlock/Tags/Author.php @@ -29,10 +29,10 @@ final class Author extends BaseTag implements Factory\StaticMethod protected $name = 'author'; /** @var string The name of the author */ - private $authorName = ''; + private $authorName; /** @var string The email of the author */ - private $authorEmail = ''; + private $authorEmail; /** * Initializes this tag with the author name and e-mail. diff --git a/src/DocBlock/Tags/Deprecated.php b/src/DocBlock/Tags/Deprecated.php index 09d57d0..2a9f1bf 100644 --- a/src/DocBlock/Tags/Deprecated.php +++ b/src/DocBlock/Tags/Deprecated.php @@ -44,7 +44,7 @@ final class Deprecated extends BaseTag implements Factory\StaticMethod )'; /** @var string|null The version vector. */ - private $version = ''; + private $version; public function __construct(?string $version = null, ?Description $description = null) { diff --git a/src/DocBlock/Tags/Example.php b/src/DocBlock/Tags/Example.php index 92668e4..8ccb4fd 100644 --- a/src/DocBlock/Tags/Example.php +++ b/src/DocBlock/Tags/Example.php @@ -34,7 +34,7 @@ final class Example implements Tag, Factory\StaticMethod * @var bool Whether the file path component represents an URI. This determines how the file portion * appears at {@link getContent()}. */ - private $isURI = false; + private $isURI; /** @var int */ private $startingLine; @@ -55,7 +55,7 @@ final class Example implements Tag, Factory\StaticMethod $this->startingLine = $startingLine; $this->lineCount = $lineCount; if ($content !== null) { - $this->content = trim((string) $content); + $this->content = trim($content); } $this->isURI = $isURI; diff --git a/src/DocBlock/Tags/InvalidTag.php b/src/DocBlock/Tags/InvalidTag.php index 5e87b2d..9f632cb 100644 --- a/src/DocBlock/Tags/InvalidTag.php +++ b/src/DocBlock/Tags/InvalidTag.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace phpDocumentor\Reflection\DocBlock\Tags; use Closure; +use Exception; use phpDocumentor\Reflection\DocBlock\Tag; use ReflectionClass; use ReflectionFunction; @@ -54,12 +55,7 @@ final class InvalidTag implements Tag return $this->name; } - /** - * @return self - * - * @inheritDoc - */ - public static function create(string $body, string $name = '') + public static function create(string $body, string $name = '') : self { return new self($name, $body); } @@ -81,7 +77,7 @@ final class InvalidTag implements Tag */ private function flattenExceptionBacktrace(Throwable $exception) : void { - $traceProperty = (new ReflectionClass('Exception'))->getProperty('trace'); + $traceProperty = (new ReflectionClass(Exception::class))->getProperty('trace'); $traceProperty->setAccessible(true); $flatten = diff --git a/src/DocBlock/Tags/Method.php b/src/DocBlock/Tags/Method.php index d22b627..e9b7a85 100644 --- a/src/DocBlock/Tags/Method.php +++ b/src/DocBlock/Tags/Method.php @@ -42,16 +42,16 @@ final class Method extends BaseTag implements Factory\StaticMethod protected $name = 'method'; /** @var string */ - private $methodName = ''; + private $methodName; /** * @phpstan-var array * @var array> */ - private $arguments = []; + private $arguments; /** @var bool */ - private $isStatic = false; + private $isStatic; /** @var Type */ private $returnType; diff --git a/src/DocBlock/Tags/Param.php b/src/DocBlock/Tags/Param.php index bd88149..48d158b 100644 --- a/src/DocBlock/Tags/Param.php +++ b/src/DocBlock/Tags/Param.php @@ -81,17 +81,19 @@ final class Param extends TagWithType implements Factory\StaticMethod // if the next item starts with a $ or ...$ it must be the variable name if (isset($parts[0]) && (strlen($parts[0]) > 0) - && ($parts[0][0] === '$' || substr($parts[0], 0, 4) === '...$') + && (strpos($parts[0], '$') === 0 || strpos($parts[0], '...$') === 0) ) { $variableName = array_shift($parts); array_shift($parts); - if ($variableName !== null && strpos($variableName, '...') === 0) { + Assert::notNull($variableName); + + if (strpos($variableName, '...') === 0) { $isVariadic = true; $variableName = substr($variableName, 3); } - if ($variableName !== null && strpos($variableName, '$') === 0) { + if (strpos($variableName, '$') === 0) { $variableName = substr($variableName, 1); } } diff --git a/src/DocBlock/Tags/Property.php b/src/DocBlock/Tags/Property.php index 27ce032..1fd1c00 100644 --- a/src/DocBlock/Tags/Property.php +++ b/src/DocBlock/Tags/Property.php @@ -34,7 +34,7 @@ use const PREG_SPLIT_DELIM_CAPTURE; final class Property extends TagWithType implements Factory\StaticMethod { /** @var string|null */ - protected $variableName = ''; + protected $variableName; public function __construct(?string $variableName, ?Type $type = null, ?Description $description = null) { @@ -71,13 +71,13 @@ final class Property extends TagWithType implements Factory\StaticMethod } // if the next item starts with a $ or ...$ it must be the variable name - if (isset($parts[0]) && ($parts[0] !== '') && (strpos($parts[0], '$') === 0)) { + if (isset($parts[0]) && strpos($parts[0], '$') === 0) { $variableName = array_shift($parts); array_shift($parts); - if ($variableName !== null && strpos($variableName, '$') === 0) { - $variableName = substr($variableName, 1); - } + Assert::notNull($variableName); + + $variableName = substr($variableName, 1); } $description = $descriptionFactory->create(implode('', $parts), $context); diff --git a/src/DocBlock/Tags/PropertyRead.php b/src/DocBlock/Tags/PropertyRead.php index 135e352..2144631 100644 --- a/src/DocBlock/Tags/PropertyRead.php +++ b/src/DocBlock/Tags/PropertyRead.php @@ -34,7 +34,7 @@ use const PREG_SPLIT_DELIM_CAPTURE; final class PropertyRead extends TagWithType implements Factory\StaticMethod { /** @var string|null */ - protected $variableName = ''; + protected $variableName; public function __construct(?string $variableName, ?Type $type = null, ?Description $description = null) { @@ -71,13 +71,13 @@ final class PropertyRead extends TagWithType implements Factory\StaticMethod } // if the next item starts with a $ or ...$ it must be the variable name - if (isset($parts[0]) && ($parts[0] !== '') && (strpos($parts[0], '$') === 0)) { + if (isset($parts[0]) && strpos($parts[0], '$') === 0) { $variableName = array_shift($parts); array_shift($parts); - if ($variableName !== null && strpos($variableName, '$') === 0) { - $variableName = substr($variableName, 1); - } + Assert::notNull($variableName); + + $variableName = substr($variableName, 1); } $description = $descriptionFactory->create(implode('', $parts), $context); diff --git a/src/DocBlock/Tags/PropertyWrite.php b/src/DocBlock/Tags/PropertyWrite.php index ad91267..3353671 100644 --- a/src/DocBlock/Tags/PropertyWrite.php +++ b/src/DocBlock/Tags/PropertyWrite.php @@ -34,7 +34,7 @@ use const PREG_SPLIT_DELIM_CAPTURE; final class PropertyWrite extends TagWithType implements Factory\StaticMethod { /** @var string */ - protected $variableName = ''; + protected $variableName; public function __construct(?string $variableName, ?Type $type = null, ?Description $description = null) { @@ -71,13 +71,13 @@ final class PropertyWrite extends TagWithType implements Factory\StaticMethod } // if the next item starts with a $ or ...$ it must be the variable name - if (isset($parts[0]) && (strlen($parts[0]) > 0) && ($parts[0][0] === '$')) { + if (isset($parts[0]) && strpos($parts[0], '$') === 0) { $variableName = array_shift($parts); array_shift($parts); - if ($variableName !== null && strpos($variableName, '$') === 0) { - $variableName = substr($variableName, 1); - } + Assert::notNull($variableName); + + $variableName = substr($variableName, 1); } $description = $descriptionFactory->create(implode('', $parts), $context); diff --git a/src/DocBlock/Tags/Since.php b/src/DocBlock/Tags/Since.php index dcc7696..d00ca38 100644 --- a/src/DocBlock/Tags/Since.php +++ b/src/DocBlock/Tags/Since.php @@ -44,7 +44,7 @@ final class Since extends BaseTag implements Factory\StaticMethod )'; /** @var string|null The version vector. */ - private $version = ''; + private $version; public function __construct(?string $version = null, ?Description $description = null) { diff --git a/src/DocBlock/Tags/Source.php b/src/DocBlock/Tags/Source.php index 2d5387f..6d3c6cb 100644 --- a/src/DocBlock/Tags/Source.php +++ b/src/DocBlock/Tags/Source.php @@ -28,7 +28,7 @@ final class Source extends BaseTag implements Factory\StaticMethod protected $name = 'source'; /** @var int The starting line, relative to the structural element's location. */ - private $startingLine = 1; + private $startingLine; /** @var int|null The number of lines, relative to the starting line. NULL means "to the end". */ private $lineCount; diff --git a/src/DocBlock/Tags/TagWithType.php b/src/DocBlock/Tags/TagWithType.php index 9cd485c..58d9416 100644 --- a/src/DocBlock/Tags/TagWithType.php +++ b/src/DocBlock/Tags/TagWithType.php @@ -39,7 +39,7 @@ abstract class TagWithType extends BaseTag { $type = ''; $nestingLevel = 0; - for ($i = 0; $i < strlen($body); $i++) { + for ($i = 0, $iMax = strlen($body); $i < $iMax; $i++) { $character = $body[$i]; if (trim($character) === '' && $nestingLevel === 0) { diff --git a/src/DocBlock/Tags/Var_.php b/src/DocBlock/Tags/Var_.php index 12c1e4b..2d867c5 100644 --- a/src/DocBlock/Tags/Var_.php +++ b/src/DocBlock/Tags/Var_.php @@ -72,13 +72,13 @@ final class Var_ extends TagWithType implements Factory\StaticMethod } // if the next item starts with a $ or ...$ it must be the variable name - if (isset($parts[0]) && ($parts[0] !== '') && (strpos($parts[0], '$') === 0)) { + if (isset($parts[0]) && strpos($parts[0], '$') === 0) { $variableName = array_shift($parts); array_shift($parts); - if ($variableName !== null && strpos($variableName, '$') === 0) { - $variableName = substr($variableName, 1); - } + Assert::notNull($variableName); + + $variableName = substr($variableName, 1); } $description = $descriptionFactory->create(implode('', $parts), $context); diff --git a/src/DocBlock/Tags/Version.php b/src/DocBlock/Tags/Version.php index 7fdb590..eaadf4d 100644 --- a/src/DocBlock/Tags/Version.php +++ b/src/DocBlock/Tags/Version.php @@ -44,7 +44,7 @@ final class Version extends BaseTag implements Factory\StaticMethod )'; /** @var string|null The version vector. */ - private $version = ''; + private $version; public function __construct(?string $version = null, ?Description $description = null) { diff --git a/src/DocBlockFactory.php b/src/DocBlockFactory.php index cc3be3c..56e2002 100644 --- a/src/DocBlockFactory.php +++ b/src/DocBlockFactory.php @@ -52,7 +52,7 @@ final class DocBlockFactory implements DocBlockFactoryInterface /** * Factory method for easy instantiation. * - * @param array> $additionalTags + * @param array> $additionalTags */ public static function createInstance(array $additionalTags = []) : self { @@ -85,6 +85,7 @@ final class DocBlockFactory implements DocBlockFactoryInterface } $docblock = $docblock->getDocComment(); + Assert::string($docblock); } Assert::stringNotEmpty($docblock); diff --git a/src/DocBlockFactoryInterface.php b/src/DocBlockFactoryInterface.php index 59e4d04..22dc859 100644 --- a/src/DocBlockFactoryInterface.php +++ b/src/DocBlockFactoryInterface.php @@ -12,7 +12,7 @@ interface DocBlockFactoryInterface /** * Factory method for easy instantiation. * - * @param array> $additionalTags + * @param array> $additionalTags */ public static function createInstance(array $additionalTags = []) : DocBlockFactory;