Browse Source

Merge branch '2.2' into 2.9

# Conflicts:
#	src/Runner/Runner.php
#	tests/AutoReview/ProjectCodeTest.php
#	tests/Console/Command/FixCommandTest.php
#	tests/Smoke/CiIntegrationTest.php
Dariusz Ruminski 7 years ago
parent
commit
84073e8726

+ 1 - 4
.travis.yml

@@ -104,10 +104,7 @@ jobs:
             php: 7.1
             install: ./dev-tools/build.sh
             script:
-                - php php-cs-fixer.phar --version
-                - php php-cs-fixer.phar readme
-                - php php-cs-fixer.phar describe header_comment
-                - php php-cs-fixer.phar fix src/Config.php -vvv --dry-run --diff
+                - vendor/bin/phpunit tests/Smoke/PharTest.php
             deploy:
                 provider: releases
                 api_key:

+ 1 - 0
composer.json

@@ -34,6 +34,7 @@
     "require-dev": {
         "johnkary/phpunit-speedtrap": "^1.1 || ^2.0@dev",
         "justinrainbow/json-schema": "^5.0",
+        "keradus/cli-executor": "^1.0",
         "mikey179/vfsStream": "^1.6",
         "php-coveralls/php-coveralls": "^2.0",
         "php-cs-fixer/accessible-object": "^1.0",

+ 4 - 0
dev-tools/build.sh

@@ -16,3 +16,7 @@ composer global show kherge/box -q || composer global require --no-interaction -
 
 # build phar file
 php -d phar.readonly=false $(composer config home)/vendor/bin/box build
+
+# revert changes to composer
+git checkout composer.json
+composer update --no-interaction --no-progress -q

+ 1 - 1
src/Console/ConfigurationResolver.php

@@ -400,7 +400,7 @@ final class ConfigurationResolver
     /**
      * @throws InvalidConfigurationException
      *
-     * @return bool
+     * @return string
      */
     public function getProgress()
     {

+ 75 - 0
src/FileReader.php

@@ -0,0 +1,75 @@
+<?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;
+
+/**
+ * File reader that unify access to regular file and stdin-alike file.
+ *
+ * Regular file could be read multiple times with `file_get_contents`, but file provided on stdin can not.
+ * Consecutive try will provide empty content for stdin-alike file.
+ * This reader unifies access to them.
+ *
+ * @internal
+ */
+final class FileReader
+{
+    /**
+     * @var ?self
+     */
+    private static $instance;
+
+    /**
+     * @var ?string
+     */
+    private $stdinContent;
+
+    /**
+     * @return self
+     */
+    public static function createSingleton()
+    {
+        if (null === self::$instance) {
+            self::$instance = new self();
+        }
+
+        return self::$instance;
+    }
+
+    /**
+     * @param string $filePath
+     *
+     * @return string
+     */
+    public function read($filePath)
+    {
+        if ('php://stdin' === $filePath) {
+            if (null === $this->stdinContent) {
+                $this->stdinContent = $this->readRaw($filePath);
+            }
+
+            return $this->stdinContent;
+        }
+
+        return $this->readRaw($filePath);
+    }
+
+    /**
+     * @param string $realPath
+     *
+     * @return string
+     */
+    private function readRaw($realPath)
+    {
+        return file_get_contents($realPath);
+    }
+}

+ 2 - 1
src/Linter/ProcessLinter.php

@@ -12,6 +12,7 @@
 
 namespace PhpCsFixer\Linter;
 
+use PhpCsFixer\FileReader;
 use PhpCsFixer\FileRemoval;
 use Symfony\Component\Filesystem\Exception\IOException;
 use Symfony\Component\Process\PhpExecutableFinder;
@@ -115,7 +116,7 @@ final class ProcessLinter implements LinterInterface
     {
         // in case php://stdin
         if (!is_file($path)) {
-            return $this->createProcessForSource(file_get_contents($path));
+            return $this->createProcessForSource(FileReader::createSingleton()->read($path));
         }
 
         $process = $this->processBuilder->build($path);

+ 2 - 1
src/Linter/TokenizerLinter.php

@@ -12,6 +12,7 @@
 
 namespace PhpCsFixer\Linter;
 
+use PhpCsFixer\FileReader;
 use PhpCsFixer\Tokenizer\CodeHasher;
 use PhpCsFixer\Tokenizer\Tokens;
 
@@ -44,7 +45,7 @@ final class TokenizerLinter implements LinterInterface
      */
     public function lintFile($path)
     {
-        return $this->lintSource(file_get_contents($path));
+        return $this->lintSource(FileReader::createSingleton()->read($path));
     }
 
     /**

+ 2 - 1
src/Runner/FileFilterIterator.php

@@ -13,6 +13,7 @@
 namespace PhpCsFixer\Runner;
 
 use PhpCsFixer\Cache\CacheManagerInterface;
+use PhpCsFixer\FileReader;
 use PhpCsFixer\FixerFileProcessedEvent;
 use Symfony\Component\EventDispatcher\Event;
 use Symfony\Component\EventDispatcher\EventDispatcher;
@@ -74,7 +75,7 @@ final class FileFilterIterator extends \FilterIterator
             return false;
         }
 
-        $content = @file_get_contents($path);
+        $content = @FileReader::createSingleton()->read($path);
         if (false === $content) {
             $error = error_get_last();
 

+ 3 - 1
src/Runner/Runner.php

@@ -19,6 +19,7 @@ use PhpCsFixer\Cache\DirectoryInterface;
 use PhpCsFixer\Differ\DifferInterface;
 use PhpCsFixer\Error\Error;
 use PhpCsFixer\Error\ErrorsManager;
+use PhpCsFixer\FileReader;
 use PhpCsFixer\Fixer\FixerInterface;
 use PhpCsFixer\FixerFileProcessedEvent;
 use PhpCsFixer\Linter\LinterInterface;
@@ -163,9 +164,10 @@ final class Runner
             return;
         }
 
-        $old = file_get_contents($file->getRealPath());
+        $old = FileReader::createSingleton()->read($file->getRealPath());
 
         Tokens::setLegacyMode(false);
+
         $tokens = Tokens::fromCode($old);
         $oldHash = $tokens->getCodeHash();
 

+ 1 - 0
src/StdinFileInfo.php

@@ -31,6 +31,7 @@ final class StdinFileInfo extends \SplFileInfo
     public function getRealPath()
     {
         // So file_get_contents & friends will work.
+        // Warning - this stream is not seekable, so `file_get_contents` will work only once! Consider using `FileReader`.
         return 'php://stdin';
     }
 

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