Skip to content

Commit 2ff6414

Browse files
authored
Merge pull request #61 from PHPCSStandards/feature/config-improve-performance
Config: only determine screen width if/when needed (performance improvement)
2 parents 2418376 + 8474e71 commit 2ff6414

File tree

3 files changed

+61
-9
lines changed

3 files changed

+61
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ The file documents changes to the PHP_CodeSniffer project.
6868
- Thanks to Dan Wallis (@fredden) for the patch
6969
- Squiz.PHP.InnerFunctions sniff no longer reports on OO methods for OO structures declared within a function or closure
7070
- Thanks to @Daimona for the patch
71+
- Runtime performance improvement for PHPCS CLI users. The improvement should be most noticeable for users on Windows.
7172

7273
### Removed
7374
- Removed support for installing via PEAR

src/Config.php

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,22 @@ public function __get($name)
207207
throw new RuntimeException("ERROR: unable to get value of property \"$name\"");
208208
}
209209

210+
// Figure out what the terminal width needs to be for "auto".
211+
if ($name === 'reportWidth' && $this->settings[$name] === 'auto') {
212+
if (function_exists('shell_exec') === true) {
213+
$dimensions = shell_exec('stty size 2>&1');
214+
if (is_string($dimensions) === true && preg_match('|\d+ (\d+)|', $dimensions, $matches) === 1) {
215+
$this->settings[$name] = (int) $matches[1];
216+
}
217+
}
218+
219+
if ($this->settings[$name] === 'auto') {
220+
// If shell_exec wasn't available or didn't yield a usable value, set to the default.
221+
// This will prevent subsequent retrievals of the reportWidth from making another call to stty.
222+
$this->settings[$name] = self::DEFAULT_REPORT_WIDTH;
223+
}
224+
}
225+
210226
return $this->settings[$name];
211227

212228
}//end __get()
@@ -229,13 +245,9 @@ public function __set($name, $value)
229245

230246
switch ($name) {
231247
case 'reportWidth' :
232-
// Support auto terminal width.
233-
if ($value === 'auto' && function_exists('shell_exec') === true) {
234-
$dimensions = shell_exec('stty size 2>&1');
235-
if (is_string($dimensions) === true && preg_match('|\d+ (\d+)|', $dimensions, $matches) === 1) {
236-
$value = (int) $matches[1];
237-
break;
238-
}
248+
if (is_string($value) === true && $value === 'auto') {
249+
// Nothing to do. Leave at 'auto'.
250+
break;
239251
}
240252

241253
if (is_int($value) === true) {
@@ -246,6 +258,7 @@ public function __set($name, $value)
246258
$value = self::DEFAULT_REPORT_WIDTH;
247259
}
248260
break;
261+
249262
case 'standards' :
250263
$cleaned = [];
251264

tests/Core/AbstractMethodUnitTest.php

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use PHP_CodeSniffer\Ruleset;
1414
use PHP_CodeSniffer\Files\DummyFile;
1515
use PHPUnit\Framework\TestCase;
16+
use ReflectionProperty;
1617

1718
abstract class AbstractMethodUnitTest extends TestCase
1819
{
@@ -47,9 +48,22 @@ abstract class AbstractMethodUnitTest extends TestCase
4748
*/
4849
public static function initializeFile()
4950
{
50-
$config = new Config();
51-
$config->standards = ['PSR1'];
51+
/*
52+
* Set the static properties in the Config class to specific values for performance
53+
* and to clear out values from other tests.
54+
*/
5255

56+
self::setStaticConfigProperty('executablePaths', []);
57+
58+
// Set to a usable value to circumvent Config trying to find a phpcs.xml config file.
59+
self::setStaticConfigProperty('overriddenDefaults', ['standards' => ['PSR1']]);
60+
61+
// Set to values which prevent the test-runner user's `CodeSniffer.conf` file
62+
// from being read and influencing the tests. Also prevent an `exec()` call to stty.
63+
self::setStaticConfigProperty('configData', ['report_width' => 80]);
64+
self::setStaticConfigProperty('configDataFile', '');
65+
66+
$config = new Config();
5367
$ruleset = new Ruleset($config);
5468

5569
// Default to a file with the same name as the test class. Extension is property based.
@@ -78,9 +92,33 @@ public static function resetFile()
7892
{
7993
self::$phpcsFile = null;
8094

95+
// Reset the static properties in the Config class to their defaults to prevent tests influencing each other.
96+
self::setStaticConfigProperty('overriddenDefaults', []);
97+
self::setStaticConfigProperty('executablePaths', []);
98+
self::setStaticConfigProperty('configData', null);
99+
self::setStaticConfigProperty('configDataFile', null);
100+
81101
}//end resetFile()
82102

83103

104+
/**
105+
* Helper function to set the value of a private static property on the Config class.
106+
*
107+
* @param string $name The name of the property to set.
108+
* @param mixed $value The value to set the property to.
109+
*
110+
* @return void
111+
*/
112+
public static function setStaticConfigProperty($name, $value)
113+
{
114+
$property = new ReflectionProperty('PHP_CodeSniffer\Config', $name);
115+
$property->setAccessible(true);
116+
$property->setValue(null, $value);
117+
$property->setAccessible(false);
118+
119+
}//end setStaticConfigProperty()
120+
121+
84122
/**
85123
* Get the token pointer for a target token based on a specific comment found on the line before.
86124
*

0 commit comments

Comments
 (0)