Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect getters and setters with types #336

Merged
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
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;
}
}