diff --git a/src/Config.php b/src/Config.php index 1ae4c77ee3..d4210c74e3 100644 --- a/src/Config.php +++ b/src/Config.php @@ -207,6 +207,22 @@ public function __get($name) throw new RuntimeException("ERROR: unable to get value of property \"$name\""); } + // Figure out what the terminal width needs to be for "auto". + if ($name === 'reportWidth' && $this->settings[$name] === 'auto') { + if (function_exists('shell_exec') === true) { + $dimensions = shell_exec('stty size 2>&1'); + if (is_string($dimensions) === true && preg_match('|\d+ (\d+)|', $dimensions, $matches) === 1) { + $this->settings[$name] = (int) $matches[1]; + } + } + + if ($this->settings[$name] === 'auto') { + // If shell_exec wasn't available or didn't yield a usable value, set to the default. + // This will prevent subsequent retrievals of the reportWidth from making another call to stty. + $this->settings[$name] = self::DEFAULT_REPORT_WIDTH; + } + } + return $this->settings[$name]; }//end __get() @@ -229,13 +245,9 @@ public function __set($name, $value) switch ($name) { case 'reportWidth' : - // Support auto terminal width. - if ($value === 'auto' && function_exists('shell_exec') === true) { - $dimensions = shell_exec('stty size 2>&1'); - if (is_string($dimensions) === true && preg_match('|\d+ (\d+)|', $dimensions, $matches) === 1) { - $value = (int) $matches[1]; - break; - } + if (is_string($value) === true && $value === 'auto') { + // Nothing to do. Leave at 'auto'. + break; } if (is_int($value) === true) { @@ -246,6 +258,7 @@ public function __set($name, $value) $value = self::DEFAULT_REPORT_WIDTH; } break; + case 'standards' : $cleaned = []; diff --git a/tests/Core/AbstractMethodUnitTest.php b/tests/Core/AbstractMethodUnitTest.php index 4d4f546995..50dd63ac69 100644 --- a/tests/Core/AbstractMethodUnitTest.php +++ b/tests/Core/AbstractMethodUnitTest.php @@ -13,6 +13,7 @@ use PHP_CodeSniffer\Ruleset; use PHP_CodeSniffer\Files\DummyFile; use PHPUnit\Framework\TestCase; +use ReflectionProperty; abstract class AbstractMethodUnitTest extends TestCase { @@ -45,9 +46,22 @@ abstract class AbstractMethodUnitTest extends TestCase */ public static function setUpBeforeClass() { - $config = new Config(); - $config->standards = ['PSR1']; + /* + * Set the static properties in the Config class to specific values for performance + * and to clear out values from other tests. + */ + self::setStaticConfigProperty('executablePaths', []); + + // Set to a usable value to circumvent Config trying to find a phpcs.xml config file. + self::setStaticConfigProperty('overriddenDefaults', ['standards' => ['PSR1']]); + + // Set to values which prevent the test-runner user's `CodeSniffer.conf` file + // from being read and influencing the tests. Also prevent an `exec()` call to stty. + self::setStaticConfigProperty('configData', ['report_width' => 80]); + self::setStaticConfigProperty('configDataFile', ''); + + $config = new Config(); $ruleset = new Ruleset($config); // Default to a file with the same name as the test class. Extension is property based. @@ -74,9 +88,33 @@ public static function tearDownAfterClass() { self::$phpcsFile = null; + // Reset the static properties in the Config class to their defaults to prevent tests influencing each other. + self::setStaticConfigProperty('overriddenDefaults', []); + self::setStaticConfigProperty('executablePaths', []); + self::setStaticConfigProperty('configData', null); + self::setStaticConfigProperty('configDataFile', null); + }//end tearDownAfterClass() + /** + * Helper function to set the value of a private static property on the Config class. + * + * @param string $name The name of the property to set. + * @param mixed $value The value to set the property to. + * + * @return void + */ + public static function setStaticConfigProperty($name, $value) + { + $property = new ReflectionProperty('PHP_CodeSniffer\Config', $name); + $property->setAccessible(true); + $property->setValue(null, $value); + $property->setAccessible(false); + + }//end setStaticConfigProperty() + + /** * Get the token pointer for a target token based on a specific comment found on the line before. *