Skip to content

fix: Check that the result of pcntl_waitpid is the PID of a managed process #1126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,21 @@ jobs:
env:
PHP_CODESNIFFER_CBF: '1'

- name: "Install bashunit"
if: ${{ matrix.custom_ini == false && matrix.os == 'ubuntu-latest' }}
run: |
curl -s https://bashunit.typeddevs.com/install.sh > install.sh
chmod +x install.sh
./install.sh

- name: "Run bashunit tests"
if: ${{ matrix.custom_ini == false && matrix.os == 'ubuntu-latest' }}
run: "./lib/bashunit -p tests/EndToEnd"

# Note: The code style check is run multiple times against every PHP version
# as it also acts as an integration test.
- name: 'PHPCS: check code style without cache, no parallel'
if: ${{ matrix.custom_ini == false }}
if: ${{ matrix.custom_ini == false && matrix.os == 'windows-latest' }}
run: php "bin/phpcs" --no-cache --parallel=1

- name: Download the PHPCS phar
Expand Down
1 change: 1 addition & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<exclude-pattern>*/src/Standards/*/Tests/*\.(inc|css|js)$</exclude-pattern>
<exclude-pattern>*/tests/Core/*/*\.(inc|css|js)$</exclude-pattern>
<exclude-pattern>*/tests/Core/*/Fixtures/*\.php$</exclude-pattern>
<exclude-pattern>*/tests/EndToEnd/Files/*\.inc$</exclude-pattern>

<arg name="basepath" value="."/>
<arg name="colors"/>
Expand Down
5 changes: 3 additions & 2 deletions src/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,7 @@ public function processFile($file)
* The reporting information returned by each child process is merged
* into the main reporter class.
*
* @param array $childProcs An array of child processes to wait for.
* @param array<non-negative-int, non-empty-string> $childProcs An array of child processes to wait for.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @param array<non-negative-int, non-empty-string> $childProcs An array of child processes to wait for.
* @param array<int, string> $childProcs An array of child processes to wait for.

These annotation should not be tooling specific and non-negative-int and non-empty-string are PHPStan specific annotations.

*
* @return bool
*/
Expand All @@ -777,7 +777,8 @@ private function processChildProcs($childProcs)

while (count($childProcs) > 0) {
$pid = pcntl_waitpid(0, $status);
if ($pid <= 0) {
if ($pid <= 0 || isset($childProcs[$pid]) === false) {
// No child or a child with an unmanaged PID was returned.
continue;
}

Expand Down
1 change: 1 addition & 0 deletions tests/EndToEnd/Files/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.fixed
18 changes: 18 additions & 0 deletions tests/EndToEnd/Files/ClassOneWithoutStyleError.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/**
* Class containing no style errors according to the end-to-end tests phpcs.xml.dist.
*
* @copyright 2025 PHPCSStandards and contributors
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/

namespace PHP_CodeSniffer\Tests\EndToEnd\Files;

class ClassOneWithoutStyleError
{
private function foo()
{
return 'bar';
}
}
20 changes: 20 additions & 0 deletions tests/EndToEnd/Files/ClassTwoWithoutStyleError.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/**
* Class containing no style errors.
*
* @copyright 2025 PHPCSStandards and contributors
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/

namespace PHP_CodeSniffer\Tests\EndToEnd\Files;

class ClassTwoWithoutStyleError
{
/**
* A property.
*
* @var string
*/
private $bar = 'baz';
}
22 changes: 22 additions & 0 deletions tests/EndToEnd/Files/ClassWithStyleError.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/**
* Class containing a simple style error that phpcbf can fix.
*
* @copyright 2025 PHPCSStandards and contributors
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/

namespace PHP_CodeSniffer\Tests\EndToEnd\Files;

class ClassWithStyleError
{
/**
* The bracket for this function is misaligned and this can be automatically fixed by phpcbf.
*
* @return string
*/
private function foo() {
return 'bar';
}
}
13 changes: 13 additions & 0 deletions tests/EndToEnd/Files/phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0"?>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="PHP_CodeSniffer" xsi:noNamespaceSchemaLocation="phpcs.xsd">
<description>The coding standard for end to end tests.</description>

<rule ref="PSR12"/>

<file>.</file>

<arg name="basepath" value="."/>
<arg name="colors"/>
<arg name="parallel" value="75"/>
<arg value="p"/>
</ruleset>
38 changes: 38 additions & 0 deletions tests/EndToEnd/phpcbf_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env bash

function tear_down() {
rm -r tests/EndToEnd/Files/*.fixed
}

function test_phpcbf_is_working() {
OUTPUT="$(bin/phpcbf --no-cache --standard=tests/EndToEnd/Files/phpcs.xml.dist tests/EndToEnd/Files/ClassOneWithoutStyleError.inc tests/EndToEnd/Files/ClassTwoWithoutStyleError.inc)"

assert_successful_code
assert_contains "No violations were found" "$OUTPUT"
}

function test_phpcbf_is_working_in_parallel() {
OUTPUT="$(bin/phpcbf --no-cache --parallel=2 --standard=tests/EndToEnd/Files/phpcs.xml.dist tests/EndToEnd/Files/ClassOneWithoutStyleError.inc tests/EndToEnd/Files/ClassTwoWithoutStyleError.inc)"

assert_successful_code
assert_contains "No violations were found" "$OUTPUT"
}

function test_phpcbf_returns_error_on_issues() {
OUTPUT="$(bin/phpcbf --no-colors --no-cache --suffix=.fixed --standard=tests/EndToEnd/Files/phpcs.xml.dist tests/EndToEnd/Files/ClassWithStyleError.inc)"
assert_exit_code 1

assert_contains "F 1 / 1 (100%)" "$OUTPUT"
assert_contains "A TOTAL OF 1 ERROR WERE FIXED IN 1 FILE" "$OUTPUT"
}

function test_phpcbf_bug_1112() {
# See https://github.com/PHPCSStandards/PHP_CodeSniffer/issues/1112
if [[ "$(uname)" == "Darwin" ]]; then
# Perform some magic with `& fg` to prevent the processes from turning into a background job.
assert_successful_code "$(bash -ic 'bash --init-file <(echo "echo \"Subprocess\"") -c "bin/phpcbf --no-cache --parallel=2 --standard=tests/EndToEnd/Files/phpcs.xml.dist tests/EndToEnd/Files/ClassOneWithoutStyleError.inc tests/EndToEnd/Files/ClassTwoWithoutStyleError.inc" & fg')"
else
# This is not needed on Linux / GitHub Actions
assert_successful_code "$(bash -ic 'bash --init-file <(echo "echo \"Subprocess\"") -c "bin/phpcbf --no-cache --parallel=2 --standard=tests/EndToEnd/Files/phpcs.xml.dist tests/EndToEnd/Files/ClassOneWithoutStyleError.inc tests/EndToEnd/Files/ClassTwoWithoutStyleError.inc"')"
fi
}
28 changes: 28 additions & 0 deletions tests/EndToEnd/phpcs_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env bash

function test_phpcs_is_working() {
assert_successful_code "$(bin/phpcs --no-cache --standard=tests/EndToEnd/Files/phpcs.xml.dist tests/EndToEnd/Files/ClassOneWithoutStyleError.inc tests/EndToEnd/Files/ClassTwoWithoutStyleError.inc)"
}

function test_phpcs_is_working_in_parallel() {
assert_successful_code "$(bin/phpcs --no-cache --parallel=2 --standard=tests/EndToEnd/Files/phpcs.xml.dist tests/EndToEnd/Files/ClassOneWithoutStyleError.inc tests/EndToEnd/Files/ClassTwoWithoutStyleError.inc)"
}

function test_phpcs_returns_error_on_issues() {
OUTPUT="$(bin/phpcs --no-colors --no-cache --standard=tests/EndToEnd/Files/phpcs.xml.dist tests/EndToEnd/Files/ClassWithStyleError.inc)"
assert_exit_code 2

assert_contains "E 1 / 1 (100%)" "$OUTPUT"
assert_contains "FOUND 1 ERROR AFFECTING 1 LINE" "$OUTPUT"
}

function test_phpcs_bug_1112() {
# See https://github.com/PHPCSStandards/PHP_CodeSniffer/issues/1112
if [[ "$(uname)" == "Darwin" ]]; then
# Perform some magic with `& fg` to prevent the processes from turning into a background job.
assert_successful_code "$(bash -ic 'bash --init-file <(echo "echo \"Subprocess\"") -c "bin/phpcs --no-cache --parallel=2 --standard=tests/EndToEnd/Files/phpcs.xml.dist tests/EndToEnd/Files/ClassOneWithoutStyleError.inc tests/EndToEnd/Files/ClassTwoWithoutStyleError.inc" & fg')"
else
# This is not needed on Linux / GitHub Actions
assert_successful_code "$(bash -ic 'bash --init-file <(echo "echo \"Subprocess\"") -c "bin/phpcs --no-cache --parallel=2 --standard=tests/EndToEnd/Files/phpcs.xml.dist tests/EndToEnd/Files/ClassOneWithoutStyleError.inc tests/EndToEnd/Files/ClassTwoWithoutStyleError.inc"')"
fi
}