Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / structure / ScopeStructureTest.php
blob41a4e434faf1c6eab60ffd707c232bae9a4c35b2
1 <?php
3 use PhpParser\Node;
4 use PhpParser\Node\Expr;
5 use PhpParser\Node\Stmt;
6 use PhpParser\ParserFactory;
7 use PhpParser\PhpVersion;
9 /**
10 * @coversNothing
12 class ScopeStructureTest extends MediaWikiIntegrationTestCase {
14 public static function provideAutoloadNoFileScope() {
15 global $wgAutoloadLocalClasses;
16 $files = array_unique( $wgAutoloadLocalClasses );
17 $args = [];
18 foreach ( $files as $file ) {
19 $args[$file] = [ $file ];
21 return $args;
24 /**
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_
43 ) {
44 continue;
46 if ( $node instanceof Stmt\Expression ) {
47 $expr = $node->expr;
48 if ( $expr instanceof Expr\FuncCall ) {
49 if ( $expr->name instanceof Node\Name ) {
50 if ( in_array( $expr->name->toString(), [
51 'class_alias',
52 'define'
53 ] ) ) {
54 continue;
57 } elseif ( $expr instanceof Expr\Include_ ) {
58 if ( $expr->type === Expr\Include_::TYPE_REQUIRE_ONCE ) {
59 continue;
61 } elseif ( $expr instanceof Expr\Assign ) {
62 if ( $expr->var->name === 'maintClass' ) {
63 continue;
67 $line = $node->getLine();
68 $this->assertNull( $node, "Found file scope code in $file at line $line" );
70 $this->assertTrue( true );