Skip to content

Commit e363a65

Browse files
Halleck45gmponos
authored andcommitted
Resolve PHP7 getters / setters (#405)
1 parent 944c2f7 commit e363a65

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/Hal/Metric/Helper/RoleOfMethodDetector.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
use PhpParser\Node\Expr\Cast;
55
use PhpParser\Node\Expr\Variable;
66
use PhpParser\Node\Identifier;
7+
use PhpParser\Node\Name;
8+
use PhpParser\Node\NullableType;
79
use PhpParser\Node\Stmt\ClassMethod;
810
use PhpParser\Node\Stmt\Return_;
911

@@ -30,6 +32,12 @@ class RoleOfMethodDetector
3032
'PhpParser\\Node\\Expr\\Variable',
3133
'PhpParser\\Node\\Name',
3234
],
35+
[
36+
'PhpParser\\Node\\Stmt\\ClassMethod',
37+
'PhpParser\\Node\\Stmt\\Return_',
38+
'PhpParser\\Node\\Expr\\PropertyFetch',
39+
'PhpParser\\Node\\Expr\\Variable',
40+
],
3341
],
3442
'setter' => [
3543
[
@@ -70,6 +78,16 @@ class RoleOfMethodDetector
7078
'PhpParser\\Node\\Param',
7179
'PhpParser\\Node\\Expr\\Variable',
7280
'PhpParser\\Node\\Name',
81+
],[
82+
// function setOk(?bool $ok): self { $this->isOk = $ok; return $this; }
83+
'PhpParser\\Node\\Stmt\\ClassMethod',
84+
'PhpParser\\Node\\Stmt\\Expression',
85+
'PhpParser\\Node\\Expr\\Assign',
86+
'PhpParser\\Node\\Expr\\Variable',
87+
'PhpParser\\Node\\Expr\\PropertyFetch',
88+
'PhpParser\\Node\\Expr\\Variable',
89+
'PhpParser\\Node\\Param',
90+
'PhpParser\\Node\\Expr\\Variable',
7391
],
7492
]
7593
];
@@ -103,6 +121,16 @@ public function detects($node)
103121
return;
104122
}
105123

124+
// avoid type hint
125+
if ($node instanceof Name) {
126+
return;
127+
}
128+
129+
// avoid nullable type
130+
if ($node instanceof NullableType) {
131+
return;
132+
}
133+
106134
$fingerprintOfMethod[] = get_class($node);
107135
});
108136
$fingerprintOfMethod = array_reverse($fingerprintOfMethod);

tests/Metric/Helper/RoleOfMethodDetectorTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ public function provideExamples()
5151
$examples['setter with scalar hint and return void'] = ['setter', '<?php class A { function setName(string $name): void { $this->name = $name; } }'];
5252
$examples['getter with return object'] = ['getter', '<?php class A { function getName(): Name { return $this->name; } }'];
5353
$examples['setter with object hint and return void'] = ['setter', '<?php class A { function setName(Name $name): void { $this->name = $name; } }'];
54+
$examples['getter with return optional'] = ['getter', '<?php class A { function isOk(): ?bool { return $this->isOk; } }'];
55+
$examples['setter fluent with param optional'] = ['setter', '<?php class A { function setOk(?bool $ok): self { $this->isOk = $ok; return $this; } }'];
56+
$examples['setter fluent non typed with param optional'] = ['setter', '<?php class A { function setOk(?bool $ok) { $this->isOk = $ok; return $this; } }'];
57+
$examples['setter with param optional'] = ['setter', '<?php class A { function setOk(?bool $ok) { $this->isOk = $ok; } }'];
5458
}
5559
return $examples;
5660
}

0 commit comments

Comments
 (0)