diff --git a/generator/src/Parameter.php b/generator/src/Parameter.php index 248b8653..71d03a85 100644 --- a/generator/src/Parameter.php +++ b/generator/src/Parameter.php @@ -15,17 +15,13 @@ class Parameter * @var PhpStanType */ private $type; - /** - * @var PhpStanParameter|null - */ - private $phpStanParams; public function __construct(\SimpleXMLElement $parameter, ?PhpStanFunction $phpStanFunction) { $this->parameter = $parameter; - $this->phpStanParams = $phpStanFunction ? $phpStanFunction->getParameter($this->getParameter()) : null; + $phpStanParam = $phpStanFunction ? $phpStanFunction->getParameter($this->getParameter()) : null; - $this->type = $this->phpStanParams ? $this->phpStanParams->getType() : new PhpStanType($this->parameter->type->__toString()); + $this->type = $phpStanParam ? $phpStanParam->getType() : new PhpStanType($this->parameter->type->__toString()); } /** diff --git a/generator/src/PhpStanFunctions/PhpStanParameter.php b/generator/src/PhpStanFunctions/PhpStanParameter.php index d14fd02e..333ca2cd 100644 --- a/generator/src/PhpStanFunctions/PhpStanParameter.php +++ b/generator/src/PhpStanFunctions/PhpStanParameter.php @@ -15,19 +15,6 @@ class PhpStanParameter * @var PhpStanType */ private $type; - - /** - * @var bool - */ - private $optional = false; - /** - * @var bool - */ - private $variadic = false; - /** - * @var bool - */ - private $byReference = false; /** * Whether the parameter is "write only" (applies only to "by reference" parameters) * @var bool @@ -37,15 +24,6 @@ class PhpStanParameter public function __construct(string $name, string $type) { $writeOnly = false; - if (\strpos($name, '=') !== false) { - $this->optional = true; - } - if (\strpos($name, '...') !== false) { - $this->variadic = true; - } - if (\strpos($name, '&') !== false) { - $this->byReference = true; - } if (\strpos($name, '&w_') !== false) { $writeOnly = true; } @@ -68,42 +46,4 @@ public function getType(): PhpStanType { return $this->type; } - - /** - * @return bool - */ - public function isOptional(): bool - { - return $this->optional; - } - - /** - * @return bool - */ - public function isVariadic(): bool - { - return $this->variadic; - } - - /** - * @return bool - */ - public function isByReference(): bool - { - return $this->byReference; - } - - /** - * Whether the parameter is "write only" (applies only to "by reference" parameters) - * @return bool - */ - public function isWriteOnly(): bool - { - return $this->writeOnly; - } - - public function isNullable(): bool - { - return $this->type->isNullable(); - } } diff --git a/generator/src/PhpStanFunctions/PhpStanType.php b/generator/src/PhpStanFunctions/PhpStanType.php index 634c5697..8ec87343 100644 --- a/generator/src/PhpStanFunctions/PhpStanType.php +++ b/generator/src/PhpStanFunctions/PhpStanType.php @@ -32,6 +32,7 @@ class PhpStanType public function __construct(string $data, bool $writeOnly = false) { + //first we try to parse the type string to have a list as clean as possible. $nullable = false; $falsable = false; // Let's make the parameter nullable if it is by reference and is used only for writing. @@ -89,6 +90,7 @@ public function getDocBlockType(?int $errorType = null): string public function getSignatureType(?int $errorType = null): string { + //We edit the return type depending of the "onErrorType" of the function. For example, a function that is both nullable and "nullsy" will created a non nullable safe function. Only relevant on return type. $nullable = $errorType === Method::NULLSY_TYPE ? false : $this->nullable; $falsable = $errorType === Method::FALSY_TYPE ? false : $this->falsable; $types = $this->types; @@ -107,7 +109,7 @@ public function getSignatureType(?int $errorType = null): string } } - //if there are several types, no typehint + //if there are several distinct types, no typehint (we use distinct in case doc block contains several times the same type, for example array|array) if (count(array_unique($types)) > 1) { return ''; } elseif (\in_array('void', $types) || (count($types) === 0 && !$nullable && !$falsable)) { @@ -133,14 +135,4 @@ public function isFalsable(): bool { return $this->falsable; } - - public function removeFalsable(): void - { - $this->falsable = false; - } - - public function removeNullable(): void - { - $this->nullable = false; - } } diff --git a/generator/tests/PhpStanFunctions/PhpStanFunctionMapReaderTest.php b/generator/tests/PhpStanFunctions/PhpStanFunctionMapReaderTest.php index 7507322a..600b5402 100644 --- a/generator/tests/PhpStanFunctions/PhpStanFunctionMapReaderTest.php +++ b/generator/tests/PhpStanFunctions/PhpStanFunctionMapReaderTest.php @@ -25,9 +25,6 @@ public function testGet(): void $this->assertCount(2, $parameters); $this->assertSame('success', $parameters['success']->getName()); $this->assertSame('bool|null', $parameters['success']->getType()->getDocBlockType()); - $this->assertFalse($parameters['success']->isVariadic()); - $this->assertTrue($parameters['success']->isByReference()); - $this->assertTrue($parameters['success']->isOptional()); } //todo: find a way to test custom map