From 36fdb658aac6d503ebf2cbfed85b97318c8d26f1 Mon Sep 17 00:00:00 2001 From: pdelre Date: Mon, 17 May 2021 08:16:13 -0400 Subject: [PATCH 1/4] replace `isatty_closure.php.inc` with `symfony/polyfill-php72` implementation --- rules/DowngradePhp72/snippet/isatty_closure.php.inc | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/rules/DowngradePhp72/snippet/isatty_closure.php.inc b/rules/DowngradePhp72/snippet/isatty_closure.php.inc index bc8394d5527..fa7cdc50ec5 100644 --- a/rules/DowngradePhp72/snippet/isatty_closure.php.inc +++ b/rules/DowngradePhp72/snippet/isatty_closure.php.inc @@ -1,9 +1,17 @@ Date: Mon, 17 May 2021 08:16:46 -0400 Subject: [PATCH 2/4] use `stream_isatty` if it exists --- rules/DowngradePhp72/snippet/isatty_closure.php.inc | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/rules/DowngradePhp72/snippet/isatty_closure.php.inc b/rules/DowngradePhp72/snippet/isatty_closure.php.inc index fa7cdc50ec5..2d543e68cb7 100644 --- a/rules/DowngradePhp72/snippet/isatty_closure.php.inc +++ b/rules/DowngradePhp72/snippet/isatty_closure.php.inc @@ -1,6 +1,10 @@ Date: Mon, 17 May 2021 08:17:35 -0400 Subject: [PATCH 3/4] update `DowngradeStreamIsattyRector::getRuleDefinition` --- .../FuncCall/DowngradeStreamIsattyRector.php | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/rules/DowngradePhp72/Rector/FuncCall/DowngradeStreamIsattyRector.php b/rules/DowngradePhp72/Rector/FuncCall/DowngradeStreamIsattyRector.php index 0fce446593c..a562b34b131 100644 --- a/rules/DowngradePhp72/Rector/FuncCall/DowngradeStreamIsattyRector.php +++ b/rules/DowngradePhp72/Rector/FuncCall/DowngradeStreamIsattyRector.php @@ -49,11 +49,23 @@ class SomeClass public function run($stream) { $streamIsatty = function ($stream) { + if (\function_exists('stream_isatty')) { + return stream_isatty($stream); + } + + if (!\is_resource($stream)) { + trigger_error('stream_isatty() expects parameter 1 to be resource, '.\gettype($stream).' given', \E_USER_WARNING); + + return false; + } + if ('\\' === \DIRECTORY_SEPARATOR) { $stat = @fstat($stream); - return $stat ? 020000 === ($stat['mode'] & 0170000) : false; + // Check if formatted mode is S_IFCHR + return $stat ? 0020000 === ($stat['mode'] & 0170000) : false; } - return @posix_isatty($stream); + + return \function_exists('posix_isatty') && @posix_isatty($stream); }; $isStream = $streamIsatty($stream); } From 0f9305afbc5965806aea639ec8d65f424dfa3c68 Mon Sep 17 00:00:00 2001 From: pdelre Date: Mon, 17 May 2021 12:53:32 -0400 Subject: [PATCH 4/4] update test fixtures for `DowngradeStreamIsattyRector` --- .../Fixture/return_is.php.inc | 10 +++++++++- .../Fixture/return_or.php.inc | 10 +++++++++- .../Fixture/some_class.php.inc | 10 +++++++++- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/rules-tests/DowngradePhp72/Rector/FuncCall/DowngradeStreamIsattyRector/Fixture/return_is.php.inc b/rules-tests/DowngradePhp72/Rector/FuncCall/DowngradeStreamIsattyRector/Fixture/return_is.php.inc index 9ae0f4bdef7..8306e91a510 100644 --- a/rules-tests/DowngradePhp72/Rector/FuncCall/DowngradeStreamIsattyRector/Fixture/return_is.php.inc +++ b/rules-tests/DowngradePhp72/Rector/FuncCall/DowngradeStreamIsattyRector/Fixture/return_is.php.inc @@ -21,11 +21,19 @@ class SomeClass public function run($stream) { $streamIsatty = function ($stream) { + if (\function_exists('stream_isatty')) { + return stream_isatty($stream); + } + if (!\is_resource($stream)) { + trigger_error('stream_isatty() expects parameter 1 to be resource, ' . \gettype($stream) . ' given', \E_USER_WARNING); + return false; + } if ('\\' === \DIRECTORY_SEPARATOR) { $stat = @fstat($stream); + // Check if formatted mode is S_IFCHR return $stat ? 020000 === ($stat['mode'] & 0170000) : false; } - return @posix_isatty($stream); + return \function_exists('posix_isatty') && @posix_isatty($stream); }; return $streamIsatty($stream); } diff --git a/rules-tests/DowngradePhp72/Rector/FuncCall/DowngradeStreamIsattyRector/Fixture/return_or.php.inc b/rules-tests/DowngradePhp72/Rector/FuncCall/DowngradeStreamIsattyRector/Fixture/return_or.php.inc index a8bd4072746..b62d9ffbfa9 100644 --- a/rules-tests/DowngradePhp72/Rector/FuncCall/DowngradeStreamIsattyRector/Fixture/return_or.php.inc +++ b/rules-tests/DowngradePhp72/Rector/FuncCall/DowngradeStreamIsattyRector/Fixture/return_or.php.inc @@ -32,11 +32,19 @@ final class UnionOr public static function detectColors(): bool { $streamIsatty = function ($stream) { + if (\function_exists('stream_isatty')) { + return stream_isatty($stream); + } + if (!\is_resource($stream)) { + trigger_error('stream_isatty() expects parameter 1 to be resource, ' . \gettype($stream) . ' given', \E_USER_WARNING); + return false; + } if ('\\' === \DIRECTORY_SEPARATOR) { $stat = @fstat($stream); + // Check if formatted mode is S_IFCHR return $stat ? 020000 === ($stat['mode'] & 0170000) : false; } - return @posix_isatty($stream); + return \function_exists('posix_isatty') && @posix_isatty($stream); }; return (PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg') && getenv('NO_COLOR') === false // https://no-color.org diff --git a/rules-tests/DowngradePhp72/Rector/FuncCall/DowngradeStreamIsattyRector/Fixture/some_class.php.inc b/rules-tests/DowngradePhp72/Rector/FuncCall/DowngradeStreamIsattyRector/Fixture/some_class.php.inc index 5657c813e05..b802fa8a723 100644 --- a/rules-tests/DowngradePhp72/Rector/FuncCall/DowngradeStreamIsattyRector/Fixture/some_class.php.inc +++ b/rules-tests/DowngradePhp72/Rector/FuncCall/DowngradeStreamIsattyRector/Fixture/some_class.php.inc @@ -21,11 +21,19 @@ class SomeClass public function run($stream) { $streamIsatty = function ($stream) { + if (\function_exists('stream_isatty')) { + return stream_isatty($stream); + } + if (!\is_resource($stream)) { + trigger_error('stream_isatty() expects parameter 1 to be resource, ' . \gettype($stream) . ' given', \E_USER_WARNING); + return false; + } if ('\\' === \DIRECTORY_SEPARATOR) { $stat = @fstat($stream); + // Check if formatted mode is S_IFCHR return $stat ? 020000 === ($stat['mode'] & 0170000) : false; } - return @posix_isatty($stream); + return \function_exists('posix_isatty') && @posix_isatty($stream); }; $isStream = $streamIsatty($stream); }