Skip to content
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
8 changes: 2 additions & 6 deletions generator/src/Parameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

/**
Expand Down
60 changes: 0 additions & 60 deletions generator/src/PhpStanFunctions/PhpStanParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
Expand All @@ -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();
}
}
14 changes: 3 additions & 11 deletions generator/src/PhpStanFunctions/PhpStanType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand All @@ -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<int>|array<string>)
if (count(array_unique($types)) > 1) {
return '';
} elseif (\in_array('void', $types) || (count($types) === 0 && !$nullable && !$falsable)) {
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down