|
1
|
|
|
<?php |
|
2
|
|
|
namespace Test\Hal\Metric\Helper; |
|
3
|
|
|
|
|
4
|
|
|
use Hal\Metric\Helper\RoleOfMethodDetector; |
|
5
|
|
|
use PhpParser\Node\Stmt\Class_; |
|
6
|
|
|
use PhpParser\Node\Stmt\ClassMethod; |
|
7
|
|
|
use PhpParser\ParserFactory; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @group method |
|
11
|
|
|
* @group helper |
|
12
|
|
|
* @group parsing |
|
13
|
|
|
*/ |
|
14
|
|
|
class RoleOfMethodDetectorTest extends \PHPUnit\Framework\TestCase |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @dataProvider provideExamples |
|
18
|
|
|
* |
|
19
|
|
|
* @param string $expected |
|
20
|
|
|
* @param string $code |
|
21
|
|
|
*/ |
|
22
|
|
|
public function testICanDetectRoleOfMethod($expected, $code) |
|
23
|
|
|
{ |
|
24
|
|
|
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7); |
|
25
|
|
|
$stmt = $parser->parse($code); |
|
26
|
|
|
$this->assertNotNull($stmt); |
|
27
|
|
|
|
|
28
|
|
|
$helper = new RoleOfMethodDetector(); |
|
29
|
|
|
|
|
30
|
|
|
foreach ($stmt as $node) { |
|
|
|
|
|
|
31
|
|
|
if ($node instanceof Class_) { |
|
32
|
|
|
foreach ($node->stmts as $sub) { |
|
33
|
|
|
if ($sub instanceof ClassMethod) { |
|
34
|
|
|
$type = $helper->detects($sub); |
|
35
|
|
|
$this->assertEquals($expected, $type); |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** @return mixed[] */ |
|
43
|
|
|
public function provideExamples() |
|
44
|
|
|
{ |
|
45
|
|
|
$examples = [ |
|
46
|
|
|
'getter' => ['getter', '<?php class A { function getName(){ return $this->name; } } ?>'], |
|
47
|
|
|
'getter with string cast' => ['getter', '<?php class A { function getName(){ return (string) $this->name; } } ?>'], |
|
48
|
|
|
'getter with int cast' => ['getter', '<?php class A { function getName(){ return (int) $this->name; } } ?>'], |
|
49
|
|
|
'setter' => ['setter', '<?php class A { function setName($string){ $this->name = $name; } } ?>'], |
|
50
|
|
|
'setter with string cast' => ['setter', '<?php class A { function setName($string){ $this->name = (string) $name; } } ?>'], |
|
51
|
|
|
'setter with $this return' => ['setter', '<?php class A { function setName($string){ $this->name = (string) $name; return $this; } } ?>'], |
|
52
|
|
|
'neither setter nor getter' => [null, '<?php class A { function foo($string){ $this->name = (string) $name * 3; } } ?>'], |
|
53
|
|
|
]; |
|
54
|
|
|
if (version_compare(PHP_VERSION, '7.0.0') >= 0) { |
|
55
|
|
|
$examples['getter with return scalar'] = ['getter', '<?php class A { function getName(): string { return $this->name; } }']; |
|
56
|
|
|
$examples['setter with scalar hint and return void'] = ['setter', '<?php class A { function setName(string $name): void { $this->name = $name; } }']; |
|
57
|
|
|
$examples['getter with return object'] = ['getter', '<?php class A { function getName(): Name { return $this->name; } }']; |
|
58
|
|
|
$examples['setter with object hint and return void'] = ['setter', '<?php class A { function setName(Name $name): void { $this->name = $name; } }']; |
|
59
|
|
|
$examples['getter with return optional'] = ['getter', '<?php class A { function isOk(): ?bool { return $this->isOk; } }']; |
|
60
|
|
|
$examples['setter fluent with param optional'] = ['setter', '<?php class A { function setOk(?bool $ok): self { $this->isOk = $ok; return $this; } }']; |
|
61
|
|
|
$examples['setter fluent non typed with param optional'] = ['setter', '<?php class A { function setOk(?bool $ok) { $this->isOk = $ok; return $this; } }']; |
|
62
|
|
|
$examples['setter with param optional'] = ['setter', '<?php class A { function setOk(?bool $ok) { $this->isOk = $ok; } }']; |
|
63
|
|
|
} |
|
64
|
|
|
return $examples; |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.