| 1 |
<?php |
| 2 |
|
| 3 |
use PhpParser\Node; |
| 4 |
use PhpParser\Node\Expr; |
| 5 |
use PhpParser\Node\Stmt; |
| 6 |
use PhpParser\ParserFactory; |
| 7 |
use PhpParser\PhpVersion; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
class ScopeStructureTest extends MediaWikiUnitTestCase { |
| 13 |
|
| 14 |
public static function provideAutoloadNoFileScope() { |
| 15 |
global $wgAutoloadLocalClasses; |
| 16 |
$files = array_unique( $wgAutoloadLocalClasses ); |
| 17 |
$args = []; |
| 18 |
foreach ( $files as $file ) { |
| 19 |
$args[$file] = [ $file ]; |
| 20 |
} |
| 21 |
return $args; |
| 22 |
} |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
public function testAutoloadNoFileScope( $file ) { |
| 33 |
|
| 34 |
|
| 35 |
$version = PhpVersion::fromComponents( 8, 1 ); |
| 36 |
$parser = ( new ParserFactory )->createForVersion( $version ); |
| 37 |
$ast = $parser->parse( file_get_contents( $file ) ); |
| 38 |
foreach ( $ast as $node ) { |
| 39 |
if ( $node instanceof Stmt\ClassLike |
| 40 |
|| $node instanceof Stmt\Namespace_ |
| 41 |
|| $node instanceof Stmt\Use_ |
| 42 |
|| $node instanceof Stmt\Nop |
| 43 |
|| $node instanceof Stmt\Declare_ |
| 44 |
|| $node instanceof Stmt\Function_ |
| 45 |
) { |
| 46 |
continue; |
| 47 |
} |
| 48 |
if ( $node instanceof Stmt\Expression ) { |
| 49 |
$expr = $node->expr; |
| 50 |
if ( $expr instanceof Expr\FuncCall ) { |
| 51 |
if ( $expr->name instanceof Node\Name ) { |
| 52 |
if ( in_array( $expr->name->toString(), [ |
| 53 |
'class_alias', |
| 54 |
'define' |
| 55 |
] ) ) { |
| 56 |
continue; |
| 57 |
} |
| 58 |
} |
| 59 |
} elseif ( $expr instanceof Expr\Include_ ) { |
| 60 |
if ( $expr->type === Expr\Include_::TYPE_REQUIRE_ONCE ) { |
| 61 |
continue; |
| 62 |
} |
| 63 |
} elseif ( $expr instanceof Expr\Assign ) { |
| 64 |
if ( $expr->var->name === 'maintClass' ) { |
| 65 |
continue; |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
$line = $node->getLine(); |
| 70 |
$this->assertNull( $node, "Found file scope code in $file at line $line" ); |
| 71 |
} |
| 72 |
$this->assertTrue( true ); |
| 73 |
} |
| 74 |
} |