@@ -21,10 +21,7 @@ class PhpCsFixer extends Plugin
2121 protected $ args = '' ;
2222
2323 protected $ config = false ;
24- protected $ configs = [
25- '.php_cs ' ,
26- '.php_cs.dist ' ,
27- ];
24+ protected $ configs = [];
2825
2926 protected $ errors = false ;
3027 protected $ reportErrors = false ;
@@ -39,6 +36,11 @@ class PhpCsFixer extends Plugin
3936 */
4037 protected $ supportsUdiff = false ;
4138
39+ /**
40+ * @var string|null
41+ */
42+ protected $ version ;
43+
4244 /**
4345 * @return string
4446 */
@@ -93,17 +95,41 @@ public function __construct(Builder $builder, Build $build, array $options = [])
9395 * Run PHP CS Fixer.
9496 *
9597 * @return bool
98+ *
99+ * @throws Exception
96100 */
97101 public function execute ()
98102 {
103+ $ phpCsFixer = $ this ->executable ;
104+
105+ // Determine the version of PHP CS Fixer
106+ $ cmd = $ phpCsFixer . ' --version ' ;
107+ $ success = $ this ->builder ->executeCommand ($ cmd );
108+ $ output = $ this ->builder ->getLastOutput ();
109+ $ matches = [];
110+ if (!\preg_match ('/(\d+\.\d+\.\d+)/ ' , $ output , $ matches )) {
111+ throw new Exception ('Unable to determine the version of the PHP Coding Standards Fixer. ' );
112+ }
113+
114+ $ this ->version = $ matches [1 ];
115+ // Appeared in PHP CS Fixer 2.8.0 and used by default since 3.0.0
116+ // https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/2.19/CHANGELOG.md#changelog-for-v280
117+ $ this ->supportsUdiff = \version_compare ($ this ->version , '2.8.0 ' , '>= ' )
118+ && \version_compare ($ this ->version , '3.0.0 ' , '< ' );
119+
99120 $ directory = '' ;
100121 if (!empty ($ this ->directory )) {
101122 $ directory = $ this ->directory ;
102123 }
103124
104125 if (!$ this ->config ) {
126+ if (\version_compare ($ this ->version , '3.0.0 ' , '>= ' )) {
127+ $ this ->configs = ['.php-cs-fixer.php ' , '.php-cs-fixer.dist.php ' ];
128+ } else {
129+ $ this ->configs = ['.php_cs ' , '.php_cs.dist ' ];
130+ }
105131 foreach ($ this ->configs as $ config ) {
106- if (file_exists ($ this ->builder ->buildPath . $ config )) {
132+ if (\ file_exists ($ this ->builder ->buildPath . $ config )) {
107133 $ this ->config = true ;
108134 $ this ->args .= ' --config=./ ' . $ config ;
109135 break ;
@@ -115,20 +141,6 @@ public function execute()
115141 $ directory = '. ' ;
116142 }
117143
118- $ phpCsFixer = $ this ->executable ;
119-
120- // Determine the version of PHP CS Fixer
121- $ cmd = $ phpCsFixer . ' --version ' ;
122- $ success = $ this ->builder ->executeCommand ($ cmd );
123- $ output = $ this ->builder ->getLastOutput ();
124- $ matches = [];
125- if (preg_match ('/(\d+\.\d+\.\d+)/ ' , $ output , $ matches )) {
126- $ version = $ matches [1 ];
127- // Appeared in PHP CS Fixer 2.8.0
128- // https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/2.12/CHANGELOG.md#changelog-for-v280
129- $ this ->supportsUdiff = version_compare ($ version , '2.8.0 ' , '>= ' );
130- }
131-
132144 if ($ this ->errors ) {
133145 $ this ->args .= ' --verbose --format json --diff ' ;
134146 if ($ this ->supportsUdiff ) {
0 commit comments