Skip to content

Commit 3e31352

Browse files
committed
fixes phpmetrics#160 - name of called class when PHP5.4 syntax is used (new X)->foo()
1 parent 78366b8 commit 3e31352

File tree

3 files changed

+41
-11
lines changed

3 files changed

+41
-11
lines changed

src/Hal/Component/OOP/Extractor/CallExtractor.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,24 +43,30 @@ public function extract(&$n, TokenCollection $tokens)
4343
{
4444

4545
$token = $tokens[$n];
46+
$call = null;
4647
switch($token->getType()) {
4748
case T_PAAMAYIM_NEKUDOTAYIM:
4849
$prev = $n - 1;
4950
$value = $this->searcher->getUnder(array('::'), $prev, $tokens);
5051
if ($value === 'parent') {
5152
$extendPosition = $this->searcher->getExtendPostition($tokens);
5253
$parentName = $this->searcher->getFollowingName($extendPosition, $tokens);
53-
return $parentName;
54-
}
55-
if ($value === 'self' || $value === 'static') {
54+
$call = $parentName;
55+
} else if ($value === 'self' || $value === 'static') {
5656
$extendPosition = $this->searcher->getClassNamePosition($tokens);
5757
$className = $this->searcher->getFollowingName($extendPosition, $tokens);
58-
return $className;
58+
$call = $className;
59+
} else {
60+
$call = $value;
5961
}
60-
return $value;
62+
break;
6163
case T_NEW:
62-
return $this->searcher->getFollowingName($n, $tokens);
64+
$call = $this->searcher->getFollowingName($n, $tokens);
65+
}
66+
67+
if(null === $call) {
68+
throw new \LogicException('Classname of call not found');
6369
}
64-
throw new \LogicException('Classname of call not found');
70+
return trim($call, '()'); // fixes PHP 5.4: (new MyClass)->foo()
6571
}
6672
};

src/Hal/Component/OOP/Extractor/MethodExtractor.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,7 @@ private function extractDependencies(ReflectedMethod $method, $n, TokenCollectio
196196
case T_PAAMAYIM_NEKUDOTAYIM:
197197
case T_NEW:
198198
$call = $extractor->extract($i, $tokens);
199-
if($call == 'class') { // anonymous class
200-
// $method->pushAnonymousClass(new ReflectedAnonymousClass($method->getNamespace(), 'anonymous@class'));
201-
} else {
199+
if($call !== 'class') { // anonymous class
202200
$method->pushDependency($call);
203201
}
204202
break;

tests/Hal/Component/OOP/CallExtractorTest.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,30 @@ public function provideCalls() {
3434
, array('\ExtendClass', 13, '<?php class CurrentClass extends \ExtendClass { parent::bar(); }')
3535
);
3636
}
37-
}
37+
38+
/**
39+
* @group wip
40+
*/
41+
public function testClassMamberDirectAccessOnInstanciationIsParsed() {
42+
$code = '<?php
43+
class A {
44+
function foo() {
45+
(new B) ->doAny();
46+
}
47+
}';
48+
$searcher = new Searcher();
49+
$callExtractor = new CallExtractor($searcher);
50+
$tokens = new TokenCollection(token_get_all($code));
51+
$n = 16;
52+
//
53+
//
54+
// foreach($tokens as $index => $t) {
55+
// $y = token_name($t->getType());
56+
// var_dump("[$index] $y {$t->getvalue()}");
57+
// }
58+
// exit;
59+
$name = $callExtractor->extract($n, $tokens);
60+
$this->assertEquals('B', $name);
61+
}
62+
}
63+

0 commit comments

Comments
 (0)