Skip to content

Commit 7eef328

Browse files
committed
PHP 8.1 | Runner::processChildProcs(): fix passing null to non-nullable bug
The `DummyFile::__construct()` method expects a `string` as the first parameter, subsequently passes it to the `File::setContent()` method, which also expects a `string`, which then passes it to the `Common::detectLineEndings()` method, which again expects a `string`. Within the `Common::detectLineEndings()` method, the string is then passed to the PHP native `preg_match()` function, which (again) expects as `string` for the `$subject` parameter. When using PHPCS with parallel processing turned on, on a system which allows for parallel processing, the `DummyFile` class was, however, being instantiated in the `Runner::processChildProcs()` method with `null` as the content of the first parameter, which on PHP 8.1 leads to `preg_match(): Passing null to parameter #2 ($subject) of type string is deprecated` deprecation notices. This deprecation notice was then caught as a `RuntimeException` in the `File::setContent()` method and passed on to the `File::addWarningOnLine()`, which called the `File::addMessage()` method. The `File::addMessage()` parameter then ran into trouble as the `$this->config` property has not been set up yet, as `DummyFile::__construct()` calls `File::setContent()` before calling the `parent::__construct()` method, leading to the `Undefined array key "cache"` notices which were making the build fail. Fixed now by passing an empty string instead of `null` as the `$content` for the `DummyFile` in the `Runner::processChildProcs()` method. This then leaves one more issue: the `DummyFile::__construct()` method contains a conditional code block which was only run when `$content !== null`. As this conditional code block is also not necessary to be run when an empty string would be passed to the constructor, changing this condition to `$content !== ''` makes that the condition can still match and maintains the efficiency tweak the condition was safeguarding.
1 parent b10c327 commit 7eef328

File tree

2 files changed

+3
-3
lines changed

2 files changed

+3
-3
lines changed

src/Files/DummyFile.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function __construct($content, Ruleset $ruleset, Config $config)
3838
// This is done by including: phpcs_input_file: [file path]
3939
// as the first line of content.
4040
$path = 'STDIN';
41-
if ($content !== null) {
41+
if ($content !== '') {
4242
if (substr($content, 0, 17) === 'phpcs_input_file:') {
4343
$eolPos = strpos($content, $this->eolChar);
4444
$filename = trim(substr($content, 17, ($eolPos - 17)));

src/Runner.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,7 @@ private function processChildProcs($childProcs)
732732

733733
if (isset($childOutput) === false) {
734734
// The child process died, so the run has failed.
735-
$file = new DummyFile(null, $this->ruleset, $this->config);
735+
$file = new DummyFile('', $this->ruleset, $this->config);
736736
$file->setErrorCounts(1, 0, 0, 0);
737737
$this->printProgress($file, $totalBatches, $numProcessed);
738738
$success = false;
@@ -756,7 +756,7 @@ private function processChildProcs($childProcs)
756756
}
757757

758758
// Fake a processed file so we can print progress output for the batch.
759-
$file = new DummyFile(null, $this->ruleset, $this->config);
759+
$file = new DummyFile('', $this->ruleset, $this->config);
760760
$file->setErrorCounts(
761761
$childOutput['totalErrors'],
762762
$childOutput['totalWarnings'],

0 commit comments

Comments
 (0)