39 $contents = file_get_contents( $file );
41 $classes = array_keys( $classesInFile );
43 $this->assertCount( 1, $classes,
44 "Only one class per file in PSR-4 autoloaded classes ($file)" );
49 $abbrFileName = substr( substr( $file, strlen( $dir ) ), 0, -4 );
50 $expectedClassName = $prefix . str_replace(
'/',
'\\', $abbrFileName );
55 "Class not autoloaded properly"
61 $this->assertCount( 0, $classes );
64 if ( $aliasesInFile ) {
66 foreach ( $aliasesInFile as $alias => $class ) {
67 $this->assertArrayHasKey( $alias, $otherClasses,
68 'Alias must be in the classmap autoloader'
80 (?:final\s+)? (?:abstract\s+)? (?:class|interface|trait) \s+
81 (?P<class> [a-zA-Z0-9_]+)
83 class_alias \s* \( \s*
84 ([\'"]) (?P<original> [^\'"]+) \g{-2} \s* , \s*
85 ([\'"]) (?P<alias> [^\'"]+ ) \g{-2} \s*
88 class_alias \s* \( \s*
89 (?P<originalStatic> [a-zA-Z0-9_]+)::class \s* , \s*
90 ([\'"]) (?P<aliasString> [^\'"]+ ) \g{-2} \s*
93 /imx', $contents,
$matches, PREG_SET_ORDER );
99 ([a-zA-Z0-9_]+(\\\\[a-zA-Z0-9_]+)*)
101 /imx', $contents, $namespaceMatch );
102 $fileNamespace = $namespaceMatch ? $namespaceMatch[1] .
'\\' :
'';
108 if ( !empty( $match[
'class'] ) ) {
110 $class = $fileNamespace . $match[
'class'];
111 $classesInFile[$class] =
true;
113 if ( !empty( $match[
'original'] ) ) {
115 $aliasesInFile[$match[
'alias']] = $match[
'original'];
118 $aliasesInFile[$match[
'aliasString']] = $fileNamespace . $match[
'originalStatic'];
123 return [ $classesInFile, $aliasesInFile ];
133 $psr4Namespaces = [];
135 $psr4Namespaces[rtrim( $ns,
'\\' ) .
'\\'] = rtrim( $path,
'/' );
138 $files = array_unique( $expected );
140 foreach ( $files as $class => $file ) {
143 if ( substr( $file, 0, 1 ) !=
'/' && substr( $file, 1, 1 ) !=
':' ) {
144 $filePath =
"$IP/$file";
149 if ( !file_exists( $filePath ) ) {
150 $actual[$class] =
"[file '$filePath' does not exist]";
154 Wikimedia\suppressWarnings();
155 $contents = file_get_contents( $filePath );
156 Wikimedia\restoreWarnings();
158 if ( $contents ===
false ) {
159 $actual[$class] =
"[couldn't read file '$filePath']";
165 foreach ( $classesInFile as $className => $ignore ) {
167 $parts = explode(
'\\', $className );
168 for ( $i = count( $parts ) - 1; $i > 0; $i-- ) {
169 $ns = implode(
'\\', array_slice( $parts, 0, $i ) ) .
'\\';
170 if ( isset( $psr4Namespaces[$ns] ) ) {
171 $expectedPath = $psr4Namespaces[$ns] .
'/'
172 . implode(
'/', array_slice( $parts, $i ) )
174 if ( $filePath === $expectedPath ) {
181 $actual[$className] = $file;
187 foreach ( $aliasesInFile as $alias => $class ) {
188 if ( isset( $classesInFile[$class] ) ) {
189 $actual[$alias] = $file;
191 $actual[$alias] =
"[original class not in $file]";
197 'expected' => $expected,