Skip to content

Commit

Permalink
Detect getters and setters with types
Browse files Browse the repository at this point in the history
  • Loading branch information
UFOMelkor committed Dec 15, 2017
1 parent 08eb64b commit 4d3cc3d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 21 deletions.
52 changes: 34 additions & 18 deletions src/Hal/Metric/Helper/RoleOfMethodDetector.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php
namespace Hal\Metric\Helper;


use PhpParser\Node\Expr\Cast;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\ClassMethod;
Expand All @@ -13,24 +12,43 @@
*/
class RoleOfMethodDetector
{

/**
* @var array
*/
private $fingerprints = [
'getter' => [
'PhpParser\\Node\\Stmt\\ClassMethod',
'PhpParser\\Node\\Stmt\\Return_',
'PhpParser\\Node\\Expr\\PropertyFetch',
'PhpParser\\Node\\Expr\\Variable',
[
'PhpParser\\Node\\Stmt\\ClassMethod',
'PhpParser\\Node\\Stmt\\Return_',
'PhpParser\\Node\\Expr\\PropertyFetch',
'PhpParser\\Node\\Expr\\Variable',
],
[
'PhpParser\\Node\\Stmt\\ClassMethod',
'PhpParser\\Node\\Stmt\\Return_',
'PhpParser\\Node\\Expr\\PropertyFetch',
'PhpParser\\Node\\Expr\\Variable',
'PhpParser\\Node\\Name',
],
],
'setter' => [
'PhpParser\\Node\\Stmt\\ClassMethod',
'PhpParser\\Node\\Expr\\Assign',
'PhpParser\\Node\\Expr\\Variable',
'PhpParser\\Node\\Expr\\PropertyFetch',
'PhpParser\\Node\\Expr\\Variable',
'PhpParser\\Node\\Param',
[
'PhpParser\\Node\\Stmt\\ClassMethod',
'PhpParser\\Node\\Expr\\Assign',
'PhpParser\\Node\\Expr\\Variable',
'PhpParser\\Node\\Expr\\PropertyFetch',
'PhpParser\\Node\\Expr\\Variable',
'PhpParser\\Node\\Param',
],
[
'PhpParser\\Node\\Stmt\\ClassMethod',
'PhpParser\\Node\\Expr\\Assign',
'PhpParser\\Node\\Expr\\Variable',
'PhpParser\\Node\\Expr\\PropertyFetch',
'PhpParser\\Node\\Expr\\Variable',
'PhpParser\\Node\\Param',
'PhpParser\\Node\\Name',
]
]
];

Expand All @@ -40,8 +58,7 @@ class RoleOfMethodDetector
*/
public function detects($node)
{

if (!$node instanceof ClassMethod) {
if (! $node instanceof ClassMethod) {
return null;
}

Expand All @@ -65,13 +82,12 @@ public function detects($node)
$fingerprintOfMethod = array_reverse($fingerprintOfMethod);

// compare with database of fingerprints
foreach ($this->fingerprints as $type => $fingerprint) {
if ($fingerprint == $fingerprintOfMethod) {
foreach ($this->fingerprints as $type => $fingerprints) {
if (in_array($fingerprintOfMethod, $fingerprints, true)) {
return $type;
}
}

return null;
}

}
}
11 changes: 8 additions & 3 deletions tests/Metric/Helper/RoleOfMethodDetectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
*/
class RoleOfMethodDetectorTest extends \PHPUnit_Framework_TestCase
{

/**
* @dataProvider provideExamples
*/
Expand All @@ -38,15 +37,21 @@ public function testICanDetectRoleOfMethod($expected, $code)

public function provideExamples()
{
return [
$examples = [
['getter', '<?php class A { function getName(){ return $this->name; } } ?>'],
['getter', '<?php class A { function getName(){ return (string) $this->name; } } ?>'],
['getter', '<?php class A { function getName(){ return (int) $this->name; } } ?>'],
['setter', '<?php class A { function setName($string){ $this->name = $name; } } ?>'],
['setter', '<?php class A { function setName($string){ $this->name = (string) $name; } } ?>'],
['setter', '<?php class A { function setName($string){ $this->name = (string) $name; return $this; } } ?>'],
[null, '<?php class A { function foo($string){ $this->name = (string) $name * 3; } } ?>'],

];
if (version_compare(PHP_VERSION, '7.0.0') >= 0) {
$examples[] = ['getter', '<?php class A { function getName(): string { return $this->name; } }'];
$examples[] = ['setter', '<?php class A { function setName(string $name): void { $this->name = $name; } }'];
$examples[] = ['getter', '<?php class A { function getName(): Name { return $this->name; } }'];
$examples[] = ['setter', '<?php class A { function setName(Name $name): void { $this->name = $name; } }'];
}
return $examples;
}
}

0 comments on commit 4d3cc3d

Please sign in to comment.