Skip to content

Commit b6373bf

Browse files
committed
Added support for grouped USE statements (PHP 7.0+) and more tests
1 parent 4f56420 commit b6373bf

File tree

7 files changed

+241
-5
lines changed

7 files changed

+241
-5
lines changed

src/PhpDepend.php

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -347,9 +347,54 @@ private function readUse()
347347
$short = FALSE;
348348
while($name = $this->readIdentifier())
349349
{
350-
$short = self::generateShort($name, TRUE);
351350
$token = $this->next();
351+
$wasGroup = FALSE;
352+
353+
if($token === '{') // group statement
354+
{
355+
$wasGroup = TRUE;
356+
$nextToken = $this->readUseGroup($name, $use);
357+
358+
if ($nextToken !== NULL) {
359+
$token = $nextToken;
360+
}
361+
362+
} else {
363+
$short = self::generateShort($name, TRUE);
364+
365+
if(is_array($token))
366+
{
367+
if($token[0] === T_AS)
368+
{
369+
$short = $this->readIdentifier();
370+
$token = $this->next();
371+
}
372+
}
373+
}
374+
375+
if(!$wasGroup && ($token === ',' || $token === ';'))
376+
{
377+
$use[$short] = $name;
378+
$short = FALSE;
379+
}
380+
}
352381

382+
return $use;
383+
}
384+
385+
386+
/**
387+
* @param string
388+
* @return mixed|NULL token or NULL
389+
*/
390+
private function readUseGroup($rootName, array &$uses)
391+
{
392+
$token = NULL;
393+
$rootName = rtrim($rootName, '\\') . '\\';
394+
395+
while ($name = $this->readIdentifier()) {
396+
$short = self::generateShort($name, TRUE);
397+
$token = $this->next();
353398

354399
if(is_array($token))
355400
{
@@ -360,15 +405,19 @@ private function readUse()
360405
}
361406
}
362407

363-
if($token === ',' || $token === ';')
408+
if($token === ',' || $token === '}')
364409
{
365-
$use[$short] = $name;
410+
$uses[$short] = $rootName . $name;
366411
$short = FALSE;
367-
}
368412

413+
if ($token === '}') {
414+
$token = $this->next();
415+
break;
416+
}
417+
}
369418
}
370419

371-
return $use;
420+
return $token;
372421
}
373422

374423

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
/**
3+
* @phpversion >= 5.6
4+
*/
5+
use Tester\Assert;
6+
require __DIR__ . '/../bootstrap.php';
7+
8+
$phpdepend = new Cz\PhpDepend;
9+
10+
// http://php.net/manual/en/language.namespaces.importing.php
11+
$phpdepend->parse('<?php
12+
use const My\Full\CONSTANT;
13+
14+
$a = new CONSTANT;
15+
');
16+
17+
Assert::same(array(), $phpdepend->getClasses());
18+
Assert::same(array(
19+
'CONSTANT',
20+
), $phpdepend->getDependencies());
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/**
3+
* @phpversion >= 5.6
4+
*/
5+
use Tester\Assert;
6+
require __DIR__ . '/../bootstrap.php';
7+
8+
$phpdepend = new Cz\PhpDepend;
9+
10+
// Example #1
11+
// http://php.net/manual/en/language.namespaces.importing.php
12+
$phpdepend->parse('<?php
13+
namespace foo;
14+
use My\Full\Classname as Another;
15+
16+
// this is the same as use My\Full\NSname as NSname
17+
use My\Full\NSname;
18+
19+
// importing a global class
20+
use ArrayObject;
21+
22+
// importing a function (PHP 5.6+)
23+
use function My\Full\functionName;
24+
25+
// aliasing a function (PHP 5.6+)
26+
use function My\Full\functionName as func;
27+
28+
// importing a constant (PHP 5.6+)
29+
use const My\Full\CONSTANT;
30+
31+
$obj = new namespace\Another; // instantiates object of class foo\Another
32+
$obj = new Another; // instantiates object of class My\Full\Classname
33+
NSname\subns\func(); // calls function My\Full\NSname\subns\func
34+
$a = new ArrayObject(array(1)); // instantiates object of class ArrayObject
35+
// without the "use ArrayObject" we would instantiate an object of class foo\ArrayObject
36+
func(); // calls function My\Full\functionName
37+
echo CONSTANT; // echoes the value of My\Full\CONSTANT
38+
');
39+
40+
Assert::same(array(), $phpdepend->getClasses());
41+
Assert::same(array(
42+
'foo\Another',
43+
'My\Full\Classname',
44+
'ArrayObject',
45+
), $phpdepend->getDependencies());
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
use Tester\Assert;
3+
require __DIR__ . '/../bootstrap.php';
4+
5+
$phpdepend = new Cz\PhpDepend;
6+
7+
// Example #2
8+
// http://php.net/manual/en/language.namespaces.importing.php
9+
$phpdepend->parse('<?php
10+
use My\Full\Classname as Another, My\Full\NSname;
11+
12+
$obj = new Another; // instantiates object of class My\Full\Classname
13+
NSname\subns\func(); // calls function My\Full\NSname\subns\func
14+
');
15+
16+
Assert::same(array(), $phpdepend->getClasses());
17+
Assert::same(array(
18+
'My\Full\Classname',
19+
), $phpdepend->getDependencies());
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
use Tester\Assert;
3+
require __DIR__ . '/../bootstrap.php';
4+
5+
$phpdepend = new Cz\PhpDepend;
6+
7+
// Example #4
8+
// http://php.net/manual/en/language.namespaces.importing.php
9+
$phpdepend->parse('<?php
10+
use My\Full\Classname as Another, My\Full\NSname;
11+
12+
$obj = new Another; // instantiates object of class My\Full\Classname
13+
$obj = new \Another; // instantiates object of class Another
14+
$obj = new Another\thing; // instantiates object of class My\Full\Classname\thing
15+
$obj = new \Another\thing; // instantiates object of class Another\thing
16+
');
17+
18+
Assert::same(array(), $phpdepend->getClasses());
19+
Assert::same(array(
20+
'My\Full\Classname',
21+
'Another',
22+
'My\Full\Classname\thing',
23+
'Another\thing'
24+
), $phpdepend->getDependencies());
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
/**
3+
* @phpversion >= 5.6
4+
*/
5+
use Tester\Assert;
6+
require __DIR__ . '/../bootstrap.php';
7+
8+
$phpdepend = new Cz\PhpDepend;
9+
10+
// http://php.net/manual/en/language.namespaces.importing.php
11+
$phpdepend->parse('<?php
12+
use function some\ns\fn_a;
13+
14+
$a = new fn_a;
15+
');
16+
17+
Assert::same(array(), $phpdepend->getClasses());
18+
Assert::same(array(
19+
'fn_a',
20+
), $phpdepend->getDependencies());
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
/**
3+
* @phpversion >= 7.0
4+
*/
5+
use Tester\Assert;
6+
require __DIR__ . '/../bootstrap.php';
7+
8+
$phpdepend = new Cz\PhpDepend;
9+
10+
// Group `use` declarations
11+
// http://php.net/manual/en/language.namespaces.importing.php
12+
$phpdepend->parse('<?php
13+
use some\ns\{ClassA, ClassB, ClassC as C};
14+
use function some\ns\{fn_a, fn_b, fn_c};
15+
use const some\ns\{ConstA, ConstB, ConstC};
16+
17+
$a = new ClassA; // some\ns\ClassA
18+
$a = new ClassB; // some\ns\ClassB
19+
$a = new ClassC; // ClassC
20+
$a = new C; // some\ns\ClassC
21+
$a = new fn_a; // fn_a
22+
$a = new fn_b; // fn_b
23+
$a = new fn_c; // fn_c
24+
$a = new ConstA; // ConstA
25+
$a = new ConstB; // ConstB
26+
$a = new ConstC; // ConstC
27+
');
28+
29+
Assert::same(array(), $phpdepend->getClasses());
30+
Assert::same(array(
31+
'some\ns\ClassA',
32+
'some\ns\ClassB',
33+
'ClassC',
34+
'some\ns\ClassC',
35+
'fn_a',
36+
'fn_b',
37+
'fn_c',
38+
'ConstA',
39+
'ConstB',
40+
'ConstC',
41+
), $phpdepend->getDependencies());
42+
43+
// spaces
44+
$phpdepend->parse('<?php
45+
use some\ns\{ ClassA, ClassB, ClassC as C };
46+
47+
$a = new ClassA; // some\ns\ClassA
48+
$a = new ClassB; // some\ns\ClassB
49+
$a = new ClassC; // ClassC
50+
$a = new C; // some\ns\ClassC
51+
');
52+
53+
Assert::same(array(), $phpdepend->getClasses());
54+
Assert::same(array(
55+
'some\ns\ClassA',
56+
'some\ns\ClassB',
57+
'ClassC',
58+
'some\ns\ClassC',
59+
), $phpdepend->getDependencies());

0 commit comments

Comments
 (0)