4 use PhpParser\Node\Expr
;
5 use PhpParser\Node\Stmt
;
6 use PhpParser\ParserFactory
;
7 use PhpParser\PhpVersion
;
12 class ScopeStructureTest
extends MediaWikiIntegrationTestCase
{
14 public static function provideAutoloadNoFileScope() {
15 global $wgAutoloadLocalClasses;
16 $files = array_unique( $wgAutoloadLocalClasses );
18 foreach ( $files as $file ) {
19 $args[$file] = [ $file ];
25 * Confirm that all files in $wgAutoloadLocalClasses have no file-scope code
26 * apart from specific exemptions.
28 * This is slow (~15s).
30 * @dataProvider provideAutoloadNoFileScope
32 public function testAutoloadNoFileScope( $file ) {
33 $parser = ( new ParserFactory
)
34 ->createForVersion( PhpVersion
::fromComponents( 7, 0 ) );
35 $ast = $parser->parse( file_get_contents( $file ) );
36 foreach ( $ast as $node ) {
37 if ( $node instanceof Stmt\ClassLike
38 ||
$node instanceof Stmt\Namespace_
39 ||
$node instanceof Stmt\Use_
40 ||
$node instanceof Stmt\Nop
41 ||
$node instanceof Stmt\Declare_
42 ||
$node instanceof Stmt\Function_
46 if ( $node instanceof Stmt\Expression
) {
48 if ( $expr instanceof Expr\FuncCall
) {
49 if ( $expr->name
instanceof Node\Name
) {
50 if ( in_array( $expr->name
->toString(), [
57 } elseif ( $expr instanceof Expr\Include_
) {
58 if ( $expr->type
=== Expr\Include_
::TYPE_REQUIRE_ONCE
) {
61 } elseif ( $expr instanceof Expr\Assign
) {
62 if ( $expr->var->name
=== 'maintClass' ) {
67 $line = $node->getLine();
68 $this->assertNull( $node, "Found file scope code in $file at line $line" );
70 $this->assertTrue( true );