Skip to content

Commit 00f2136

Browse files
committed
Config: only determine screen width if/when needed
Follow up on 3761 for which tests were added in 3820. I'm not sure about *nix, but on Windows, the call to `shell_exec('stty ...')` is slow. As things were, the call to `shell_exec('stty ...')` would be: * Made when the initial default value for `reportWidth` is being set in `restoreDefaults()`. * Potentially made a second time if the users `CodeSniffer.conf` file contained a `report_width => 'auto'` entry. * Potentially made a third time if any of the rulesets used for the run contained a `<config name="report-width" value="auto"/>` entry. * Potentially made a fourth time if `--report-width=auto` would be passed from the command-line. This is inefficient for the following reasons: 1. The output from `shell_exec('stty ...')` does not change between calls (well, providing the user doesn't resize their terminal in the microseconds between calls) 2. We don't actually need to _know_ the value `'auto'` translates to, until the `reportWidth` will be _used_. Taking the above into account, making the same call up to four times is not desirable. This commit moves the translation from `'auto'` to an actual terminal width from the `__set()` method to the `__get()` method and overwrites the `reportWidth` value from `'auto'` with the actual terminal width value, if available, and with the default value if the terminal width could not be determined. This means that subsequent calls to `__get()` for the `reportWidth` will automatically use the previously determined value instead of trying to determine value again. This removes the inefficiency and should make PHPCS runs a little bit faster (at the very least on Windows). The only time multiple calls to `shell_exec('stty...')` could now need to be made, would be if the `reportWidth` would be changed (back to `'auto'`) between the first retrieval and a subsequent retrieval of the `reportWidth` value. As things are, this will never happen in a normal PHPCS run, though could possibly happen in a test situation.
1 parent 2418376 commit 00f2136

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
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

0 commit comments

Comments
 (0)