Skip to content

Commit 5b782da

Browse files
authored
Merge pull request #151 from Kharhamel/cleanup
code cleanup
2 parents 4c86a21 + 760622f commit 5b782da

File tree

4 files changed

+5
-80
lines changed

4 files changed

+5
-80
lines changed

generator/src/Parameter.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,13 @@ class Parameter
1515
* @var PhpStanType
1616
*/
1717
private $type;
18-
/**
19-
* @var PhpStanParameter|null
20-
*/
21-
private $phpStanParams;
2218

2319
public function __construct(\SimpleXMLElement $parameter, ?PhpStanFunction $phpStanFunction)
2420
{
2521
$this->parameter = $parameter;
26-
$this->phpStanParams = $phpStanFunction ? $phpStanFunction->getParameter($this->getParameter()) : null;
22+
$phpStanParam = $phpStanFunction ? $phpStanFunction->getParameter($this->getParameter()) : null;
2723

28-
$this->type = $this->phpStanParams ? $this->phpStanParams->getType() : new PhpStanType($this->parameter->type->__toString());
24+
$this->type = $phpStanParam ? $phpStanParam->getType() : new PhpStanType($this->parameter->type->__toString());
2925
}
3026

3127
/**

generator/src/PhpStanFunctions/PhpStanParameter.php

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,6 @@ class PhpStanParameter
1515
* @var PhpStanType
1616
*/
1717
private $type;
18-
19-
/**
20-
* @var bool
21-
*/
22-
private $optional = false;
23-
/**
24-
* @var bool
25-
*/
26-
private $variadic = false;
27-
/**
28-
* @var bool
29-
*/
30-
private $byReference = false;
3118
/**
3219
* Whether the parameter is "write only" (applies only to "by reference" parameters)
3320
* @var bool
@@ -37,15 +24,6 @@ class PhpStanParameter
3724
public function __construct(string $name, string $type)
3825
{
3926
$writeOnly = false;
40-
if (\strpos($name, '=') !== false) {
41-
$this->optional = true;
42-
}
43-
if (\strpos($name, '...') !== false) {
44-
$this->variadic = true;
45-
}
46-
if (\strpos($name, '&') !== false) {
47-
$this->byReference = true;
48-
}
4927
if (\strpos($name, '&w_') !== false) {
5028
$writeOnly = true;
5129
}
@@ -68,42 +46,4 @@ public function getType(): PhpStanType
6846
{
6947
return $this->type;
7048
}
71-
72-
/**
73-
* @return bool
74-
*/
75-
public function isOptional(): bool
76-
{
77-
return $this->optional;
78-
}
79-
80-
/**
81-
* @return bool
82-
*/
83-
public function isVariadic(): bool
84-
{
85-
return $this->variadic;
86-
}
87-
88-
/**
89-
* @return bool
90-
*/
91-
public function isByReference(): bool
92-
{
93-
return $this->byReference;
94-
}
95-
96-
/**
97-
* Whether the parameter is "write only" (applies only to "by reference" parameters)
98-
* @return bool
99-
*/
100-
public function isWriteOnly(): bool
101-
{
102-
return $this->writeOnly;
103-
}
104-
105-
public function isNullable(): bool
106-
{
107-
return $this->type->isNullable();
108-
}
10949
}

generator/src/PhpStanFunctions/PhpStanType.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class PhpStanType
3232

3333
public function __construct(string $data, bool $writeOnly = false)
3434
{
35+
//first we try to parse the type string to have a list as clean as possible.
3536
$nullable = false;
3637
$falsable = false;
3738
// 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
8990

9091
public function getSignatureType(?int $errorType = null): string
9192
{
93+
//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.
9294
$nullable = $errorType === Method::NULLSY_TYPE ? false : $this->nullable;
9395
$falsable = $errorType === Method::FALSY_TYPE ? false : $this->falsable;
9496
$types = $this->types;
@@ -107,7 +109,7 @@ public function getSignatureType(?int $errorType = null): string
107109
}
108110
}
109111

110-
//if there are several types, no typehint
112+
//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>)
111113
if (count(array_unique($types)) > 1) {
112114
return '';
113115
} elseif (\in_array('void', $types) || (count($types) === 0 && !$nullable && !$falsable)) {
@@ -133,14 +135,4 @@ public function isFalsable(): bool
133135
{
134136
return $this->falsable;
135137
}
136-
137-
public function removeFalsable(): void
138-
{
139-
$this->falsable = false;
140-
}
141-
142-
public function removeNullable(): void
143-
{
144-
$this->nullable = false;
145-
}
146138
}

generator/tests/PhpStanFunctions/PhpStanFunctionMapReaderTest.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@ public function testGet(): void
2525
$this->assertCount(2, $parameters);
2626
$this->assertSame('success', $parameters['success']->getName());
2727
$this->assertSame('bool|null', $parameters['success']->getType()->getDocBlockType());
28-
$this->assertFalse($parameters['success']->isVariadic());
29-
$this->assertTrue($parameters['success']->isByReference());
30-
$this->assertTrue($parameters['success']->isOptional());
3128
}
3229

3330
//todo: find a way to test custom map

0 commit comments

Comments
 (0)