Browse Source

Merge branch '2.2' into 2.7

# Conflicts:
#	src/Console/Command/FixCommand.php
#	src/ToolInfo.php
#	tests/AutoReview/ProjectFixerConfigurationTest.php
#	tests/Cache/CacheTest.php
#	tests/ConfigTest.php
#	tests/Console/ConfigurationResolverTest.php
#	tests/ToolInfoTest.php
Dariusz Ruminski 7 years ago
parent
commit
23127f89af

+ 11 - 3
src/Console/Application.php

@@ -17,6 +17,7 @@ use PhpCsFixer\Console\Command\FixCommand;
 use PhpCsFixer\Console\Command\HelpCommand;
 use PhpCsFixer\Console\Command\HelpCommand;
 use PhpCsFixer\Console\Command\ReadmeCommand;
 use PhpCsFixer\Console\Command\ReadmeCommand;
 use PhpCsFixer\Console\Command\SelfUpdateCommand;
 use PhpCsFixer\Console\Command\SelfUpdateCommand;
+use PhpCsFixer\ToolInfo;
 use Symfony\Component\Console\Application as BaseApplication;
 use Symfony\Component\Console\Application as BaseApplication;
 use Symfony\Component\Console\Command\ListCommand;
 use Symfony\Component\Console\Command\ListCommand;
 use Symfony\Component\Console\Input\InputInterface;
 use Symfony\Component\Console\Input\InputInterface;
@@ -34,16 +35,23 @@ final class Application extends BaseApplication
     const VERSION = '2.7.5-DEV';
     const VERSION = '2.7.5-DEV';
     const VERSION_CODENAME = 'Sandy Pool';
     const VERSION_CODENAME = 'Sandy Pool';
 
 
+    /**
+     * @var ToolInfo
+     */
+    private $toolInfo;
+
     public function __construct()
     public function __construct()
     {
     {
         error_reporting(-1);
         error_reporting(-1);
 
 
         parent::__construct('PHP CS Fixer', self::VERSION);
         parent::__construct('PHP CS Fixer', self::VERSION);
 
 
+        $this->toolInfo = new ToolInfo();
+
         $this->add(new DescribeCommand());
         $this->add(new DescribeCommand());
-        $this->add(new FixCommand());
+        $this->add(new FixCommand($this->toolInfo));
         $this->add(new ReadmeCommand());
         $this->add(new ReadmeCommand());
-        $this->add(new SelfUpdateCommand());
+        $this->add(new SelfUpdateCommand($this->toolInfo));
     }
     }
 
 
     /**
     /**
@@ -56,7 +64,7 @@ final class Application extends BaseApplication
             : ($input->hasParameterOption('--format', true) && 'txt' !== $input->getParameterOption('--format', null, true) ? null : $output)
             : ($input->hasParameterOption('--format', true) && 'txt' !== $input->getParameterOption('--format', null, true) ? null : $output)
         ;
         ;
         if (null !== $stdErr) {
         if (null !== $stdErr) {
-            $warningsDetector = new WarningsDetector();
+            $warningsDetector = new WarningsDetector($this->toolInfo);
             $warningsDetector->detectOldVendor();
             $warningsDetector->detectOldVendor();
             $warningsDetector->detectOldMajor();
             $warningsDetector->detectOldMajor();
             if (FixCommand::COMMAND_NAME === $this->getCommandName($input)) {
             if (FixCommand::COMMAND_NAME === $this->getCommandName($input)) {

+ 10 - 2
src/Console/Command/FixCommand.php

@@ -21,6 +21,7 @@ use PhpCsFixer\Console\Output\ProcessOutput;
 use PhpCsFixer\Error\ErrorsManager;
 use PhpCsFixer\Error\ErrorsManager;
 use PhpCsFixer\Report\ReportSummary;
 use PhpCsFixer\Report\ReportSummary;
 use PhpCsFixer\Runner\Runner;
 use PhpCsFixer\Runner\Runner;
+use PhpCsFixer\ToolInfoInterface;
 use Symfony\Component\Console\Command\Command;
 use Symfony\Component\Console\Command\Command;
 use Symfony\Component\Console\Input\InputArgument;
 use Symfony\Component\Console\Input\InputArgument;
 use Symfony\Component\Console\Input\InputInterface;
 use Symfony\Component\Console\Input\InputInterface;
@@ -61,7 +62,12 @@ final class FixCommand extends Command
      */
      */
     private $defaultConfig;
     private $defaultConfig;
 
 
-    public function __construct()
+    /**
+     * @var ToolInfoInterface
+     */
+    private $toolInfo;
+
+    public function __construct(ToolInfoInterface $toolInfo)
     {
     {
         parent::__construct();
         parent::__construct();
 
 
@@ -69,6 +75,7 @@ final class FixCommand extends Command
         $this->errorsManager = new ErrorsManager();
         $this->errorsManager = new ErrorsManager();
         $this->eventDispatcher = new EventDispatcher();
         $this->eventDispatcher = new EventDispatcher();
         $this->stopwatch = new Stopwatch();
         $this->stopwatch = new Stopwatch();
+        $this->toolInfo = $toolInfo;
     }
     }
 
 
     /**
     /**
@@ -135,7 +142,8 @@ final class FixCommand extends Command
                 'verbosity' => $verbosity,
                 'verbosity' => $verbosity,
                 'show-progress' => $input->getOption('show-progress'),
                 'show-progress' => $input->getOption('show-progress'),
             ],
             ],
-            getcwd()
+            getcwd(),
+            $this->toolInfo
         );
         );
 
 
         $reporter = $resolver->getReporter();
         $reporter = $resolver->getReporter();

+ 14 - 2
src/Console/Command/SelfUpdateCommand.php

@@ -14,7 +14,7 @@ namespace PhpCsFixer\Console\Command;
 
 
 use PhpCsFixer\Console\SelfUpdate\GithubClient;
 use PhpCsFixer\Console\SelfUpdate\GithubClient;
 use PhpCsFixer\Console\SelfUpdate\NewVersionChecker;
 use PhpCsFixer\Console\SelfUpdate\NewVersionChecker;
-use PhpCsFixer\ToolInfo;
+use PhpCsFixer\ToolInfoInterface;
 use Symfony\Component\Console\Command\Command;
 use Symfony\Component\Console\Command\Command;
 use Symfony\Component\Console\Input\InputInterface;
 use Symfony\Component\Console\Input\InputInterface;
 use Symfony\Component\Console\Input\InputOption;
 use Symfony\Component\Console\Input\InputOption;
@@ -33,6 +33,18 @@ final class SelfUpdateCommand extends Command
 {
 {
     const COMMAND_NAME = 'self-update';
     const COMMAND_NAME = 'self-update';
 
 
+    /**
+     * @var ToolInfoInterface
+     */
+    private $toolInfo;
+
+    public function __construct(ToolInfoInterface $toolInfo)
+    {
+        parent::__construct();
+
+        $this->toolInfo = $toolInfo;
+    }
+
     /**
     /**
      * {@inheritdoc}
      * {@inheritdoc}
      */
      */
@@ -65,7 +77,7 @@ EOT
      */
      */
     protected function execute(InputInterface $input, OutputInterface $output)
     protected function execute(InputInterface $input, OutputInterface $output)
     {
     {
-        if (!ToolInfo::isInstalledAsPhar()) {
+        if (!$this->toolInfo->isInstalledAsPhar()) {
             $output->writeln('<error>Self-update is available only for PHAR version.</error>');
             $output->writeln('<error>Self-update is available only for PHAR version.</error>');
 
 
             return 1;
             return 1;

+ 15 - 7
src/Console/ConfigurationResolver.php

@@ -33,7 +33,7 @@ use PhpCsFixer\Report\ReporterFactory;
 use PhpCsFixer\Report\ReporterInterface;
 use PhpCsFixer\Report\ReporterInterface;
 use PhpCsFixer\RuleSet;
 use PhpCsFixer\RuleSet;
 use PhpCsFixer\StdinFileInfo;
 use PhpCsFixer\StdinFileInfo;
-use PhpCsFixer\ToolInfo;
+use PhpCsFixer\ToolInfoInterface;
 use PhpCsFixer\WhitespacesFixerConfig;
 use PhpCsFixer\WhitespacesFixerConfig;
 use PhpCsFixer\WordMatcher;
 use PhpCsFixer\WordMatcher;
 use Symfony\Component\Console\Output\OutputInterface;
 use Symfony\Component\Console\Output\OutputInterface;
@@ -104,6 +104,11 @@ final class ConfigurationResolver
      */
      */
     private $configFinderIsOverridden;
     private $configFinderIsOverridden;
 
 
+    /**
+     * @var ToolInfoInterface
+     */
+    private $toolInfo;
+
     /**
     /**
      * @var array
      * @var array
      */
      */
@@ -143,17 +148,20 @@ final class ConfigurationResolver
     /**
     /**
      * ConfigurationResolver constructor.
      * ConfigurationResolver constructor.
      *
      *
-     * @param ConfigInterface $config
-     * @param array           $options
-     * @param string          $cwd
+     * @param ConfigInterface   $config
+     * @param array             $options
+     * @param string            $cwd
+     * @param ToolInfoInterface $toolInfo
      */
      */
     public function __construct(
     public function __construct(
         ConfigInterface $config,
         ConfigInterface $config,
         array $options,
         array $options,
-        $cwd
+        $cwd,
+        ToolInfoInterface $toolInfo
     ) {
     ) {
         $this->cwd = $cwd;
         $this->cwd = $cwd;
         $this->defaultConfig = $config;
         $this->defaultConfig = $config;
+        $this->toolInfo = $toolInfo;
 
 
         foreach ($options as $name => $value) {
         foreach ($options as $name => $value) {
             $this->setOption($name, $value);
             $this->setOption($name, $value);
@@ -186,12 +194,12 @@ final class ConfigurationResolver
     public function getCacheManager()
     public function getCacheManager()
     {
     {
         if (null === $this->cacheManager) {
         if (null === $this->cacheManager) {
-            if ($this->getUsingCache() && (ToolInfo::isInstalledAsPhar() || ToolInfo::isInstalledByComposer())) {
+            if ($this->getUsingCache() && ($this->toolInfo->isInstalledAsPhar() || $this->toolInfo->isInstalledByComposer())) {
                 $this->cacheManager = new FileCacheManager(
                 $this->cacheManager = new FileCacheManager(
                     new FileHandler($this->getCacheFile()),
                     new FileHandler($this->getCacheFile()),
                     new Signature(
                     new Signature(
                         PHP_VERSION,
                         PHP_VERSION,
-                        ToolInfo::getVersion(),
+                        $this->toolInfo->getVersion(),
                         $this->getRules()
                         $this->getRules()
                     ),
                     ),
                     $this->isDryRun(),
                     $this->isDryRun(),

+ 13 - 2
src/Console/WarningsDetector.php

@@ -13,6 +13,7 @@
 namespace PhpCsFixer\Console;
 namespace PhpCsFixer\Console;
 
 
 use PhpCsFixer\ToolInfo;
 use PhpCsFixer\ToolInfo;
+use PhpCsFixer\ToolInfoInterface;
 
 
 /**
 /**
  * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
@@ -21,11 +22,21 @@ use PhpCsFixer\ToolInfo;
  */
  */
 final class WarningsDetector
 final class WarningsDetector
 {
 {
+    /**
+     * @var ToolInfoInterface
+     */
+    private $toolInfo;
+
     /**
     /**
      * @var string[]
      * @var string[]
      */
      */
     private $warnings = [];
     private $warnings = [];
 
 
+    public function __construct(ToolInfoInterface $toolInfo)
+    {
+        $this->toolInfo = $toolInfo;
+    }
+
     public function detectOldMajor()
     public function detectOldMajor()
     {
     {
         // @TODO to be activated at v3
         // @TODO to be activated at v3
@@ -34,8 +45,8 @@ final class WarningsDetector
 
 
     public function detectOldVendor()
     public function detectOldVendor()
     {
     {
-        if (ToolInfo::isInstalledByComposer()) {
-            $details = ToolInfo::getComposerInstallationDetails();
+        if ($this->toolInfo->isInstalledByComposer()) {
+            $details = $this->toolInfo->getComposerInstallationDetails();
             if (ToolInfo::COMPOSER_LEGACY_PACKAGE_NAME === $details['name']) {
             if (ToolInfo::COMPOSER_LEGACY_PACKAGE_NAME === $details['name']) {
                 $this->warnings[] = sprintf(
                 $this->warnings[] = sprintf(
                     'You are running PHP CS Fixer installed with old vendor `%s`. Please update to `%s`.',
                     'You are running PHP CS Fixer installed with old vendor `%s`. Please update to `%s`.',

+ 30 - 35
src/ToolInfo.php

@@ -21,79 +21,74 @@ use PhpCsFixer\Console\Application;
  *
  *
  * @internal
  * @internal
  */
  */
-final class ToolInfo
+final class ToolInfo implements ToolInfoInterface
 {
 {
     const COMPOSER_PACKAGE_NAME = 'friendsofphp/php-cs-fixer';
     const COMPOSER_PACKAGE_NAME = 'friendsofphp/php-cs-fixer';
 
 
     const COMPOSER_LEGACY_PACKAGE_NAME = 'fabpot/php-cs-fixer';
     const COMPOSER_LEGACY_PACKAGE_NAME = 'fabpot/php-cs-fixer';
 
 
-    public static function getComposerInstallationDetails()
-    {
-        static $result;
+    /**
+     * @var null|array
+     */
+    private $composerInstallationDetails;
+
+    /**
+     * @var null|bool
+     */
+    private $isInstalledByComposer;
 
 
-        if (!self::isInstalledByComposer()) {
+    public function getComposerInstallationDetails()
+    {
+        if (!$this->isInstalledByComposer()) {
             throw new \LogicException('Cannot get composer version for tool not installed by composer.');
             throw new \LogicException('Cannot get composer version for tool not installed by composer.');
         }
         }
 
 
-        if (null === $result) {
-            $composerInstalled = json_decode(file_get_contents(self::getComposerInstalledFile()), true);
+        if (null === $this->composerInstallationDetails) {
+            $composerInstalled = json_decode(file_get_contents($this->getComposerInstalledFile()), true);
 
 
             foreach ($composerInstalled as $package) {
             foreach ($composerInstalled as $package) {
                 if (in_array($package['name'], [self::COMPOSER_PACKAGE_NAME, self::COMPOSER_LEGACY_PACKAGE_NAME], true)) {
                 if (in_array($package['name'], [self::COMPOSER_PACKAGE_NAME, self::COMPOSER_LEGACY_PACKAGE_NAME], true)) {
-                    $result = $package;
+                    $this->composerInstallationDetails = $package;
 
 
                     break;
                     break;
                 }
                 }
             }
             }
         }
         }
 
 
-        return $result;
+        return $this->composerInstallationDetails;
     }
     }
 
 
-    public static function getComposerVersion()
+    public function getComposerVersion()
     {
     {
-        static $result;
-
-        if (null === $result) {
-            $package = self::getComposerInstallationDetails();
-            $result = $package['version'].'#'.$package['dist']['reference'];
-        }
+        $package = $this->getComposerInstallationDetails();
 
 
-        return $result;
+        return $package['version'].'#'.$package['dist']['reference'];
     }
     }
 
 
-    public static function getVersion()
+    public function getVersion()
     {
     {
-        if (self::isInstalledByComposer()) {
-            return Application::VERSION.':'.self::getComposerVersion();
+        if ($this->isInstalledByComposer()) {
+            return Application::VERSION.':'.$this->getComposerVersion();
         }
         }
 
 
         return Application::VERSION;
         return Application::VERSION;
     }
     }
 
 
-    public static function isInstalledAsPhar()
+    public function isInstalledAsPhar()
     {
     {
-        static $result;
-
-        if (null === $result) {
-            $result = 'phar://' === substr(__DIR__, 0, 7);
-        }
-
-        return $result;
+        return 'phar://' === substr(__DIR__, 0, 7);
     }
     }
 
 
-    public static function isInstalledByComposer()
+    public function isInstalledByComposer()
     {
     {
-        static $result;
-
-        if (null === $result) {
-            $result = !self::isInstalledAsPhar() && file_exists(self::getComposerInstalledFile());
+        if (null === $this->isInstalledByComposer) {
+            $this->isInstalledByComposer = !$this->isInstalledAsPhar() && file_exists($this->getComposerInstalledFile());
         }
         }
 
 
-        return $result;
+        return $this->isInstalledByComposer;
     }
     }
 
 
-    private static function getComposerInstalledFile()
+    private function getComposerInstalledFile()
     {
     {
         return __DIR__.'/../../../composer/installed.json';
         return __DIR__.'/../../../composer/installed.json';
     }
     }

+ 29 - 0
src/ToolInfoInterface.php

@@ -0,0 +1,29 @@
+<?php
+
+/*
+ * This file is part of PHP CS Fixer.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *     Dariusz Rumiński <dariusz.ruminski@gmail.com>
+ *
+ * This source file is subject to the MIT license that is bundled
+ * with this source code in the file LICENSE.
+ */
+
+namespace PhpCsFixer;
+
+/**
+ * @internal
+ */
+interface ToolInfoInterface
+{
+    public function getComposerInstallationDetails();
+
+    public function getComposerVersion();
+
+    public function getVersion();
+
+    public function isInstalledAsPhar();
+
+    public function isInstalledByComposer();
+}

+ 8 - 1
tests/AutoReview/ProjectFixerConfigurationTest.php

@@ -13,6 +13,7 @@
 namespace PhpCsFixer\Tests\AutoReview;
 namespace PhpCsFixer\Tests\AutoReview;
 
 
 use PhpCsFixer\Console\ConfigurationResolver;
 use PhpCsFixer\Console\ConfigurationResolver;
+use PhpCsFixer\ToolInfo;
 use PHPUnit\Framework\TestCase;
 use PHPUnit\Framework\TestCase;
 
 
 /**
 /**
@@ -35,7 +36,13 @@ final class ProjectFixerConfigurationTest extends TestCase
         $this->assertNotEmpty($config->getRules());
         $this->assertNotEmpty($config->getRules());
 
 
         // call so the fixers get configured to reveal issue (like deprecated configuration used etc.)
         // call so the fixers get configured to reveal issue (like deprecated configuration used etc.)
-        $resolver = new ConfigurationResolver($config, [], __DIR__);
+        $resolver = new ConfigurationResolver(
+            $config,
+            [],
+            __DIR__,
+            new ToolInfo()
+        );
+
         $resolver->getFixers();
         $resolver->getFixers();
     }
     }
 }
 }

+ 3 - 1
tests/Cache/CacheTest.php

@@ -172,6 +172,8 @@ final class CacheTest extends TestCase
 
 
     public function provideCanConvertToAndFromJsonCases()
     public function provideCanConvertToAndFromJsonCases()
     {
     {
+        $toolInfo = new ToolInfo();
+
         return [
         return [
             [new Signature(
             [new Signature(
                 PHP_VERSION,
                 PHP_VERSION,
@@ -183,7 +185,7 @@ final class CacheTest extends TestCase
             )],
             )],
             [new Signature(
             [new Signature(
                 PHP_VERSION,
                 PHP_VERSION,
-                ToolInfo::getVersion(),
+                $toolInfo->getVersion(),
                 [
                 [
                     // value encoded in ANSI, not UTF
                     // value encoded in ANSI, not UTF
                     'header_comment' => ['header' => 'Dariusz '.base64_decode('UnVtafFza2k=', true)],
                     'header_comment' => ['header' => 'Dariusz '.base64_decode('UnVtafFza2k=', true)],

+ 9 - 4
tests/ConfigTest.php

@@ -18,6 +18,7 @@ use PhpCsFixer\Console\Command\FixCommand;
 use PhpCsFixer\Console\ConfigurationResolver;
 use PhpCsFixer\Console\ConfigurationResolver;
 use PhpCsFixer\Finder;
 use PhpCsFixer\Finder;
 use PhpCsFixer\Fixer\FixerInterface;
 use PhpCsFixer\Fixer\FixerInterface;
+use PhpCsFixer\ToolInfo;
 use PHPUnit\Framework\TestCase;
 use PHPUnit\Framework\TestCase;
 use Symfony\Component\Console\Output\OutputInterface;
 use Symfony\Component\Console\Output\OutputInterface;
 use Symfony\Component\Console\Tester\CommandTester;
 use Symfony\Component\Console\Tester\CommandTester;
@@ -38,7 +39,8 @@ final class ConfigTest extends TestCase
             [
             [
                 'rules' => 'cast_spaces,braces',
                 'rules' => 'cast_spaces,braces',
             ],
             ],
-            getcwd()
+            getcwd(),
+            new ToolInfo()
         );
         );
 
 
         $this->assertArraySubset(
         $this->assertArraySubset(
@@ -58,7 +60,8 @@ final class ConfigTest extends TestCase
             [
             [
                 'rules' => '{"array_syntax": {"syntax": "short"}, "cast_spaces": true}',
                 'rules' => '{"array_syntax": {"syntax": "short"}, "cast_spaces": true}',
             ],
             ],
-            getcwd()
+            getcwd(),
+            new ToolInfo()
         );
         );
 
 
         $this->assertArraySubset(
         $this->assertArraySubset(
@@ -82,7 +85,8 @@ final class ConfigTest extends TestCase
             [
             [
                 'rules' => '{blah',
                 'rules' => '{blah',
             ],
             ],
-            getcwd()
+            getcwd(),
+            new ToolInfo()
         );
         );
         $configResolver->getRules();
         $configResolver->getRules();
     }
     }
@@ -92,9 +96,10 @@ final class ConfigTest extends TestCase
         $customConfigFile = __DIR__.'/Fixtures/.php_cs_custom.php';
         $customConfigFile = __DIR__.'/Fixtures/.php_cs_custom.php';
 
 
         $application = new Application();
         $application = new Application();
-        $application->add(new FixCommand());
+        $application->add(new FixCommand(new ToolInfo()));
 
 
         $commandTester = new CommandTester($application->find('fix'));
         $commandTester = new CommandTester($application->find('fix'));
+
         $commandTester->execute(
         $commandTester->execute(
             [
             [
                 'path' => [$customConfigFile],
                 'path' => [$customConfigFile],

Some files were not shown because too many files changed in this diff