Make static anlisys pass

This commit is contained in:
Jaapio
2019-12-10 23:46:06 +01:00
parent 09e086ecf1
commit c14c01875a
24 changed files with 143 additions and 96 deletions
+4 -2
View File
@@ -177,8 +177,8 @@ final class StandardTagFactory implements TagFactory
public function registerTagHandler(string $tagName, string $handler) : void
{
Assert::stringNotEmpty($tagName);
Assert::stringNotEmpty($handler);
Assert::classExists($handler);
/** @var object $handler stupid hack to make phpstan happy. */
Assert::implementsInterface($handler, StaticMethod::class);
if (strpos($tagName, '\\') && $tagName[0] !== '\\') {
@@ -224,7 +224,9 @@ final class StandardTagFactory implements TagFactory
);
try {
return call_user_func_array([$handlerClassName, 'create'], $arguments);
/** @var callable $callable */
$callable = [$handlerClassName, 'create'];
return call_user_func_array($callable, $arguments);
} catch (InvalidArgumentException $e) {
return null;
}
+2 -5
View File
@@ -24,7 +24,7 @@ abstract class BaseTag implements DocBlock\Tag
/** @var string Name of the tag */
protected $name = '';
/** @var Description|string|null Description of the tag. */
/** @var Description|null Description of the tag. */
protected $description;
/**
@@ -37,10 +37,7 @@ abstract class BaseTag implements DocBlock\Tag
return $this->name;
}
/**
* @return Description|string|null
*/
public function getDescription()
public function getDescription() : ?Description
{
return $this->description;
}
+1 -1
View File
@@ -94,6 +94,6 @@ final class Deprecated extends BaseTag implements Factory\StaticMethod
*/
public function __toString() : string
{
return $this->version . ($this->description ? ' ' . $this->description->render() : '');
return ($this->version ?? '') . ($this->description ? ' ' . $this->description->render() : '');
}
}
+32 -18
View File
@@ -13,7 +13,6 @@ declare(strict_types=1);
namespace phpDocumentor\Reflection\DocBlock\Tags;
use phpDocumentor\Reflection\DocBlock\Description;
use phpDocumentor\Reflection\DocBlock\Tag;
use Webmozart\Assert\Assert;
use function array_key_exists;
@@ -26,7 +25,7 @@ use function trim;
/**
* Reflection class for a {@}example tag in a Docblock.
*/
final class Example extends BaseTag
final class Example implements Tag
{
/** @var string Path to a file to use as an example. May also be an absolute URI. */
private $filePath;
@@ -43,10 +42,10 @@ final class Example extends BaseTag
/** @var int */
private $lineCount;
/**
* @param string|Description|null $description
*/
public function __construct(string $filePath, bool $isURI, int $startingLine, int $lineCount, $description)
/** @var string|null */
private $content;
public function __construct(string $filePath, bool $isURI, int $startingLine, int $lineCount, ?string $content)
{
Assert::notEmpty($filePath);
Assert::greaterThanEq($startingLine, 0);
@@ -55,20 +54,16 @@ final class Example extends BaseTag
$this->filePath = $filePath;
$this->startingLine = $startingLine;
$this->lineCount = $lineCount;
$this->name = 'example';
if ($description !== null) {
$this->description = trim((string) $description);
if ($content !== null) {
$this->content = trim((string) $content);
}
$this->isURI = $isURI;
}
/**
* {@inheritdoc}
*/
public function getContent()
public function getContent() : string
{
if ($this->description === null) {
if ($this->content === null) {
$filePath = '"' . $this->filePath . '"';
if ($this->isURI) {
$filePath = $this->isUriRelative($this->filePath)
@@ -76,10 +71,15 @@ final class Example extends BaseTag
: $this->filePath;
}
return trim($filePath . ' ' . parent::getDescription());
return trim($filePath);
}
return $this->description;
return $this->content;
}
public function getDescription() : ?string
{
return $this->content;
}
/**
@@ -121,7 +121,7 @@ final class Example extends BaseTag
}
return new static(
$filePath ?? $fileUri,
$filePath ?? ($fileUri ?? ''),
$fileUri !== null,
$startingLine,
$lineCount,
@@ -145,7 +145,7 @@ final class Example extends BaseTag
*/
public function __toString() : string
{
return $this->filePath . ($this->description ? ' ' . $this->description : '');
return $this->filePath . ($this->content ? ' ' . $this->content : '');
}
/**
@@ -165,4 +165,18 @@ final class Example extends BaseTag
{
return $this->lineCount;
}
public function getName() : string
{
return 'example';
}
public function render(?Formatter $formatter = null) : string
{
if ($formatter === null) {
$formatter = new Formatter\PassthroughFormatter();
}
return $formatter->format($this);
}
}
+1 -1
View File
@@ -24,7 +24,7 @@ use function preg_match;
/**
* Parses a tag definition for a DocBlock.
*/
class Generic extends BaseTag implements Factory\StaticMethod
final class Generic extends BaseTag implements Factory\StaticMethod
{
/**
* Parses a tag and populates the member variables.
+13 -7
View File
@@ -27,7 +27,6 @@ use function implode;
use function is_string;
use function preg_match;
use function sort;
use function strlen;
use function strpos;
use function substr;
use function trim;
@@ -54,7 +53,9 @@ final class Method extends BaseTag implements Factory\StaticMethod
private $returnType;
/**
* @param mixed[][] $arguments $arguments
* @param mixed[][] $arguments
*
* @psalm-param array<int, array<string, string|Type>|string> $arguments
*/
public function __construct(
string $methodName,
@@ -64,7 +65,6 @@ final class Method extends BaseTag implements Factory\StaticMethod
?Description $description = null
) {
Assert::stringNotEmpty($methodName);
Assert::boolean($static);
if ($returnType === null) {
$returnType = new Void_();
@@ -151,7 +151,7 @@ final class Method extends BaseTag implements Factory\StaticMethod
$returnType = $typeResolver->resolve($returnType, $context);
$description = $descriptionFactory->create($description, $context);
if (is_string($arguments) && strlen($arguments) > 0) {
if ($arguments !== '') {
$arguments = explode(',', $arguments);
foreach ($arguments as &$argument) {
$argument = explode(' ', self::stripRestArg(trim($argument)), 2);
@@ -222,13 +222,17 @@ final class Method extends BaseTag implements Factory\StaticMethod
}
/**
* @param mixed[][] $arguments
* @param mixed[][]|string[] $arguments
*
* @return mixed[][]
*
* @psalm-param array<int, array<string, string|Type>|string> $arguments
* @psalm-return array<int, array<string, string|Type>> $arguments
*/
private function filterArguments(array $arguments = []) : array
{
foreach ($arguments as &$argument) {
$result = [];
foreach ($arguments as $argument) {
if (is_string($argument)) {
$argument = ['name' => $argument];
}
@@ -244,9 +248,11 @@ final class Method extends BaseTag implements Factory\StaticMethod
'Arguments can only have the "name" and "type" fields, found: ' . var_export($keys, true)
);
}
$result[] = $argument;
}
return $arguments;
return $result;
}
private static function stripRestArg(string $argument) : string
+6 -6
View File
@@ -37,14 +37,14 @@ final class Param extends BaseTag implements Factory\StaticMethod
/** @var Type|null */
private $type;
/** @var string */
private $variableName = '';
/** @var string|null */
private $variableName;
/** @var bool determines whether this is a variadic argument */
private $isVariadic = false;
private $isVariadic;
public function __construct(
string $variableName,
?string $variableName,
?Type $type = null,
bool $isVariadic = false,
?Description $description = null
@@ -105,7 +105,7 @@ final class Param extends BaseTag implements Factory\StaticMethod
/**
* Returns the variable's name.
*/
public function getVariableName() : string
public function getVariableName() : ?string
{
return $this->variableName;
}
@@ -133,7 +133,7 @@ final class Param extends BaseTag implements Factory\StaticMethod
{
return ($this->type ? $this->type . ' ' : '')
. ($this->isVariadic() ? '...' : '')
. '$' . $this->variableName
. ($this->variableName !== null ? '$' . $this->variableName : '')
. ($this->description ? ' ' . $this->description : '');
}
}
+5 -5
View File
@@ -29,7 +29,7 @@ use function substr;
/**
* Reflection class for a {@}property tag in a Docblock.
*/
class Property extends BaseTag implements Factory\StaticMethod
final class Property extends BaseTag implements Factory\StaticMethod
{
/** @var string */
protected $name = 'property';
@@ -37,10 +37,10 @@ class Property extends BaseTag implements Factory\StaticMethod
/** @var Type|null */
private $type;
/** @var string */
/** @var string|null */
protected $variableName = '';
public function __construct(string $variableName, ?Type $type = null, ?Description $description = null)
public function __construct(?string $variableName, ?Type $type = null, ?Description $description = null)
{
$this->variableName = $variableName;
$this->type = $type;
@@ -89,7 +89,7 @@ class Property extends BaseTag implements Factory\StaticMethod
/**
* Returns the variable's name.
*/
public function getVariableName() : string
public function getVariableName() : ?string
{
return $this->variableName;
}
@@ -108,7 +108,7 @@ class Property extends BaseTag implements Factory\StaticMethod
public function __toString() : string
{
return ($this->type ? $this->type . ' ' : '')
. '$' . $this->variableName
. ($this->variableName ? '$' . $this->variableName : '')
. ($this->description ? ' ' . $this->description : '');
}
}
+5 -5
View File
@@ -29,7 +29,7 @@ use function substr;
/**
* Reflection class for a {@}property-read tag in a Docblock.
*/
class PropertyRead extends BaseTag implements Factory\StaticMethod
final class PropertyRead extends BaseTag implements Factory\StaticMethod
{
/** @var string */
protected $name = 'property-read';
@@ -37,10 +37,10 @@ class PropertyRead extends BaseTag implements Factory\StaticMethod
/** @var Type|null */
private $type;
/** @var string */
/** @var string|null */
protected $variableName = '';
public function __construct(string $variableName, ?Type $type = null, ?Description $description = null)
public function __construct(?string $variableName, ?Type $type = null, ?Description $description = null)
{
$this->variableName = $variableName;
$this->type = $type;
@@ -89,7 +89,7 @@ class PropertyRead extends BaseTag implements Factory\StaticMethod
/**
* Returns the variable's name.
*/
public function getVariableName() : string
public function getVariableName() : ?string
{
return $this->variableName;
}
@@ -108,7 +108,7 @@ class PropertyRead extends BaseTag implements Factory\StaticMethod
public function __toString() : string
{
return ($this->type ? $this->type . ' ' : '')
. '$' . $this->variableName
. ($this->variableName ? '$' . $this->variableName : '')
. ($this->description ? ' ' . $this->description : '');
}
}
+5 -5
View File
@@ -30,7 +30,7 @@ use function substr;
/**
* Reflection class for a {@}property-write tag in a Docblock.
*/
class PropertyWrite extends BaseTag implements Factory\StaticMethod
final class PropertyWrite extends BaseTag implements Factory\StaticMethod
{
/** @var string */
protected $name = 'property-write';
@@ -38,10 +38,10 @@ class PropertyWrite extends BaseTag implements Factory\StaticMethod
/** @var Type|null */
private $type;
/** @var string */
/** @var string|null */
protected $variableName = '';
public function __construct(string $variableName, ?Type $type = null, ?Description $description = null)
public function __construct(?string $variableName, ?Type $type = null, ?Description $description = null)
{
$this->variableName = $variableName;
$this->type = $type;
@@ -90,7 +90,7 @@ class PropertyWrite extends BaseTag implements Factory\StaticMethod
/**
* Returns the variable's name.
*/
public function getVariableName() : string
public function getVariableName() : ?string
{
return $this->variableName;
}
@@ -109,7 +109,7 @@ class PropertyWrite extends BaseTag implements Factory\StaticMethod
public function __toString() : string
{
return ($this->type ? $this->type . ' ' : '')
. '$' . $this->variableName
. ($this->variableName ? '$' . $this->variableName : '')
. ($this->description ? ' ' . $this->description : '');
}
}
+1 -1
View File
@@ -69,6 +69,6 @@ final class Return_ extends BaseTag implements Factory\StaticMethod
public function __toString() : string
{
return $this->type . ' ' . $this->description;
return $this->type . ' ' . (string) $this->description;
}
}
+1 -1
View File
@@ -27,7 +27,7 @@ use function preg_split;
/**
* Reflection class for an {@}see tag in a Docblock.
*/
class See extends BaseTag implements Factory\StaticMethod
final class See extends BaseTag implements Factory\StaticMethod
{
/** @var string */
protected $name = 'see';
+1 -1
View File
@@ -88,6 +88,6 @@ final class Since extends BaseTag implements Factory\StaticMethod
*/
public function __toString() : string
{
return $this->version . ($this->description ? ' ' . $this->description->render() : '');
return (string) $this->version . ($this->description ? ' ' . (string) $this->description : '');
}
}
+2 -2
View File
@@ -72,7 +72,7 @@ final class Source extends BaseTag implements Factory\StaticMethod
$description = $matches[3];
}
return new static($startingLine, $lineCount, $descriptionFactory->create($description, $context));
return new static($startingLine, $lineCount, $descriptionFactory->create($description??'', $context));
}
/**
@@ -101,6 +101,6 @@ final class Source extends BaseTag implements Factory\StaticMethod
{
return $this->startingLine
. ($this->lineCount !== null ? ' ' . $this->lineCount : '')
. ($this->description ? ' ' . $this->description->render() : '');
. ($this->description ? ' ' . (string) $this->description : '');
}
}
+1 -1
View File
@@ -69,6 +69,6 @@ final class Throws extends BaseTag implements Factory\StaticMethod
public function __toString() : string
{
return $this->type . ' ' . $this->description;
return (string) $this->type . ' ' . (string) $this->description;
}
}
+1 -1
View File
@@ -76,6 +76,6 @@ final class Uses extends BaseTag implements Factory\StaticMethod
*/
public function __toString() : string
{
return $this->refers . ' ' . $this->description->render();
return $this->refers . ' ' . (string) $this->description;
}
}
+5 -7
View File
@@ -29,7 +29,7 @@ use function substr;
/**
* Reflection class for a {@}var tag in a Docblock.
*/
class Var_ extends BaseTag implements Factory\StaticMethod
final class Var_ extends BaseTag implements Factory\StaticMethod
{
/** @var string */
protected $name = 'var';
@@ -37,10 +37,10 @@ class Var_ extends BaseTag implements Factory\StaticMethod
/** @var Type|null */
private $type;
/** @var string */
/** @var string|null */
protected $variableName = '';
public function __construct(string $variableName, ?Type $type = null, ?Description $description = null)
public function __construct(?string $variableName, ?Type $type = null, ?Description $description = null)
{
$this->variableName = $variableName;
$this->type = $type;
@@ -68,9 +68,7 @@ class Var_ extends BaseTag implements Factory\StaticMethod
// if the first item that is encountered is not a variable; it is a type
if (isset($parts[0]) && ($parts[0] !== '') && ($parts[0][0] !== '$')) {
if ($typeResolver !== null) {
$type = $typeResolver->resolve(array_shift($parts), $context);
}
$type = $typeResolver->resolve(array_shift($parts), $context);
array_shift($parts);
}
@@ -92,7 +90,7 @@ class Var_ extends BaseTag implements Factory\StaticMethod
/**
* Returns the variable's name.
*/
public function getVariableName() : string
public function getVariableName() : ?string
{
return $this->variableName;
}