@@ -9,9 +9,6 @@ class Checker
99{
1010 private string $ fileListPath ;
1111
12- /** @var string[] Default preset versions if detection fails */
13- private array $ presetVersions = ['8.1 ' , '8.0 ' , '7.4 ' , '7.3 ' , '7.1 ' ];
14-
1512
1613 public function __construct (
1714 private string $ vendorDir ,
@@ -35,23 +32,13 @@ public function setPaths(array $paths): void
3532 'vendor ' ,
3633 ])
3734 ->filter (fn (SplFileInfo $ file ) => !preg_match ('#@phpVersion\s+([0-9.]+)#i ' , file_get_contents ((string ) $ file ), $ m )
38- || version_compare (PHP_VERSION , $ m [1 ], '>= ' ))
35+ || version_compare (PHP_VERSION , $ m [1 ], '>= ' ))
3936 ->in ($ paths );
4037
4138 file_put_contents ($ this ->fileListPath , implode ("\n" , iterator_to_array ($ finder )));
4239 }
4340
4441
45- /**
46- * Derives a preset name (e.g., 'php81') from a PHP version string (e.g., '8.1').
47- */
48- public function derivePresetFromVersion (?string $ phpVersion = null ): string
49- {
50- $ phpVersion ??= $ this ->detectPhpVersion () ?? end ($ this ->presetVersions );
51- return $ this ->preset = 'php ' . str_replace ('. ' , '' , $ phpVersion );
52- }
53-
54-
5542 /**
5643 * Runs PHP CS Fixer.
5744 * Returns true on success, false on failure.
@@ -61,7 +48,12 @@ public function runFixer(): bool
6148 $ fixerBin = $ this ->vendorDir . '/friendsofphp/php-cs-fixer/php-cs-fixer ' ;
6249
6350 $ presetPath = dirname (__DIR__ ) . '/preset-fixer ' ;
64- $ presetFile = "$ presetPath/ $ this ->preset .php " ;
51+ $ preset = $ this ->preset ;
52+ if ($ preset === null ) {
53+ $ preset = $ this ->derivePresetFromVersion ($ presetPath );
54+ echo "Preset: $ preset detected from PHP version \n" ;
55+ }
56+ $ presetFile = "$ presetPath/ $ preset.php " ;
6557 if (!is_file ($ presetFile )) {
6658 fwrite (STDERR , "Error: Preset configuration not found for PHP CS Fixer: {$ presetFile }\n" );
6759 return false ;
@@ -86,7 +78,12 @@ public function runSniffer(): bool
8678 $ snifferBin = $ this ->vendorDir . '/squizlabs/php_codesniffer/bin/ ' . ($ this ->dryRun ? 'phpcs ' : 'phpcbf ' );
8779
8880 $ presetPath = dirname (__DIR__ ) . '/preset-sniffer ' ;
89- $ presetFile = "$ presetPath/ $ this ->preset .xml " ;
81+ $ preset = $ this ->preset ;
82+ if ($ preset === null ) {
83+ $ preset = $ this ->derivePresetFromVersion ($ presetPath );
84+ echo "Preset: $ preset detected from PHP version \n" ;
85+ }
86+ $ presetFile = "$ presetPath/ $ preset.xml " ;
9087 if (!is_file ($ presetFile )) {
9188 fwrite (STDERR , "Error: Preset ruleset not found for PHP_CodeSniffer: {$ presetFile }\n" );
9289 return false ;
@@ -97,7 +94,7 @@ public function runSniffer(): bool
9794 $ ncsPath = $ this ->projectDir . '/ncs.xml ' ;
9895
9996 try {
100- if (preg_match ('~php(\d)(\d)~ ' , $ this -> preset , $ m )) {
97+ if (preg_match ('~php(\d)(\d)~ ' , $ preset , $ m )) {
10198 $ phpVersionOption = " --runtime-set php_version {$ m [1 ]}0 {$ m [2 ]}00 " ;
10299 if (is_file ($ ncsPath )) {
103100 echo "Using custom ruleset: $ ncsPath \n" ;
@@ -134,6 +131,26 @@ public function runSniffer(): bool
134131 }
135132
136133
134+ /**
135+ * Derives a preset name (e.g., 'php81') from a PHP version.
136+ */
137+ private function derivePresetFromVersion (string $ path ): string
138+ {
139+ $ phpVersion = $ this ->detectPhpVersion ();
140+ $ versions = array_map (
141+ fn ($ file ) => preg_match ('/php(\d)(\d+)\.\w+$/ ' , $ file , $ m ) ? "$ m [1 ]. $ m [2 ]" : null ,
142+ glob ("$ path/php* " ),
143+ );
144+ usort ($ versions , fn ($ a , $ b ) => -version_compare ($ a , $ b ));
145+ foreach ($ versions as $ version ) {
146+ if (version_compare ($ version , $ phpVersion ?? '0 ' , '<= ' )) {
147+ break ;
148+ }
149+ }
150+ return 'php ' . str_replace ('. ' , '' , $ version );
151+ }
152+
153+
137154 /**
138155 * Tries to detect the required PHP version from composer.json.
139156 */
0 commit comments