Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
85.29% |
29 / 34 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
ExtensionInfo | |
85.29% |
29 / 34 |
|
75.00% |
3 / 4 |
12.46 | |
0.00% |
0 / 1 |
newFromFile | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
supportsMediaWiki | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
4 | |||
readInfo | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
5 |
1 | <?php |
2 | /** |
3 | * Copyright (C) 2017 Kunal Mehta <legoktm@member.fsf.org> |
4 | * |
5 | * This program is free software; you can redistribute it and/or modify |
6 | * it under the terms of the GNU General Public License as published by |
7 | * the Free Software Foundation; either version 2 of the License, or |
8 | * (at your option) any later version. |
9 | * |
10 | * This program is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | * GNU General Public License for more details. |
14 | * |
15 | * You should have received a copy of the GNU General Public License along |
16 | * with this program; if not, write to the Free Software Foundation, Inc., |
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
18 | */ |
19 | |
20 | namespace MediaWiki\Sniffs\Utils; |
21 | |
22 | use Composer\Semver\Constraint\Constraint; |
23 | use Composer\Semver\VersionParser; |
24 | use PHP_CodeSniffer\Files\File; |
25 | |
26 | class ExtensionInfo { |
27 | |
28 | /** |
29 | * @var string Extension root path |
30 | */ |
31 | private string $dir; |
32 | |
33 | /** |
34 | * @var array|false|null Parsed extension.json |
35 | */ |
36 | private $info = null; |
37 | |
38 | /** @var bool[] */ |
39 | private array $supportCache = []; |
40 | |
41 | /** |
42 | * @param File $phpcsFile |
43 | * |
44 | * @return ExtensionInfo |
45 | */ |
46 | public static function newFromFile( File $phpcsFile ) { |
47 | static $instances = []; |
48 | // The first standard path will be .phpcs.xml in the extension root |
49 | $dir = dirname( $phpcsFile->config->standards[0] ); |
50 | if ( !isset( $instances[$dir] ) ) { |
51 | $instances[$dir] = new self( $dir ); |
52 | } |
53 | |
54 | return $instances[$dir]; |
55 | } |
56 | |
57 | /** |
58 | * @internal For tests only, use ExtensionInfo::newFromFile instead |
59 | * @param string $dir Path of extension |
60 | */ |
61 | public function __construct( $dir ) { |
62 | $this->dir = $dir; |
63 | } |
64 | |
65 | /** |
66 | * @param string $version Version to see if it is still supported |
67 | * |
68 | * @return bool |
69 | */ |
70 | public function supportsMediaWiki( $version ) { |
71 | if ( isset( $this->supportCache[$version] ) ) { |
72 | return $this->supportCache[$version]; |
73 | } |
74 | |
75 | $info = $this->readInfo(); |
76 | if ( !$info ) { |
77 | // Default behavior is that we assume they're following master |
78 | return false; |
79 | } |
80 | |
81 | if ( !isset( $info['requires']['MediaWiki'] ) ) { |
82 | return false; |
83 | } |
84 | |
85 | $versionParser = new VersionParser(); |
86 | $ourVersion = new Constraint( '==', $versionParser->normalize( $version ) ); |
87 | $ourVersion->setPrettyString( $version ); |
88 | $matches = $versionParser |
89 | ->parseConstraints( $info['requires']['MediaWiki'] ) |
90 | ->matches( $ourVersion ); |
91 | $this->supportCache[$version] = $matches; |
92 | return $matches; |
93 | } |
94 | |
95 | private function readInfo() { |
96 | if ( $this->info !== null ) { |
97 | return $this->info; |
98 | } |
99 | |
100 | $found = false; |
101 | foreach ( [ 'extension', 'skin' ] as $type ) { |
102 | $path = "{$this->dir}/$type.json"; |
103 | if ( file_exists( $path ) ) { |
104 | $found = true; |
105 | break; |
106 | } |
107 | } |
108 | |
109 | if ( !$found ) { |
110 | $this->info = false; |
111 | return $this->info; |
112 | } |
113 | |
114 | $this->info = json_decode( file_get_contents( $path ), true ); |
115 | return $this->info; |
116 | } |
117 | |
118 | } |