From 16755eadd42b521b4698bd3e4fdf8cb7357d40cb Mon Sep 17 00:00:00 2001 From: Dmitry Khomutov Date: Sun, 4 Feb 2018 12:44:41 +0700 Subject: [PATCH] Code style fixes + more tests for invalid UTF-8 log. --- src/PHPCensor/Builder.php | 8 ++-- src/PHPCensor/Helper/CommandExecutor.php | 38 ++++++++++--------- .../Helper/CommandExecutorInterface.php | 8 ++-- src/PHPCensor/Plugin.php | 6 +-- .../PHPCensor/Helper/CommandExecutorTest.php | 26 ++++++++++++- 5 files changed, 56 insertions(+), 30 deletions(-) diff --git a/src/PHPCensor/Builder.php b/src/PHPCensor/Builder.php index 16c15bce..5cbe6778 100644 --- a/src/PHPCensor/Builder.php +++ b/src/PHPCensor/Builder.php @@ -308,11 +308,11 @@ class Builder implements LoggerAwareInterface /** * Find a binary required by a plugin. * - * @param string $binary - * @param bool $quiet Returns null instead of throwing an exception. - * @param string $priorityPath + * @param array|string $binary + * @param bool $quiet Returns null instead of throwing an exception. + * @param string $priorityPath * - * @return null|string + * @return string|false * * @throws \Exception when no binary has been found and $quiet is false. */ diff --git a/src/PHPCensor/Helper/CommandExecutor.php b/src/PHPCensor/Helper/CommandExecutor.php index d1e366ff..77e05069 100644 --- a/src/PHPCensor/Helper/CommandExecutor.php +++ b/src/PHPCensor/Helper/CommandExecutor.php @@ -36,6 +36,9 @@ class CommandExecutor implements CommandExecutorInterface */ protected $lastError; + /** + * @var boolean + */ public $logExecOutput = true; /** @@ -100,7 +103,6 @@ class CommandExecutor implements CommandExecutorInterface $lastOutput = ''; $lastError = ''; - if (is_resource($process)) { fclose($pipes[0]); @@ -109,7 +111,7 @@ class CommandExecutor implements CommandExecutorInterface $status = proc_close($process); $lastOutput = $this->replaceIllegalCharacters($lastOutput); - $lastError = $this->replaceIllegalCharacters($lastError); + $lastError = $this->replaceIllegalCharacters($lastError); } $this->lastOutput = array_filter(explode(PHP_EOL, $lastOutput)); @@ -138,7 +140,7 @@ class CommandExecutor implements CommandExecutorInterface /** * Reads from array of streams as data becomes available. * - * @param array $descriptors + * @param array $descriptors * * @return string[] data read from each descriptor */ @@ -167,20 +169,20 @@ class CommandExecutor implements CommandExecutorInterface return $outputs; } - private static function replaceIllegalCharacters($utf8String) + /** + * @param string $utf8String + * + * @return string + */ + public function replaceIllegalCharacters($utf8String) { - $substCharCode = 65533; - mb_substitute_character($substCharCode); - $legalUtf8String = mb_convert_encoding($utf8String, 'utf8', 'utf8'); - $regexp = '/[\x00-\x08\x10\x0B\x0C\x0E-\x19\x7F]' . '|[\x00-\x7F][\x80-\xBF]+' . '|([\xC0\xC1]|[\xF0-\xFF])[\x80-\xBF]*' . '|[\xC2-\xDF]((?![\x80-\xBF])|[\x80-\xBF]{2,})' . '|[\xE0-\xEF](([\x80-\xBF](?![\x80-\xBF]))|(?![\x80-\xBF]{2})|[\x80-\xBF]{3,})/S'; - $cleanUtf8String = preg_replace($regexp, chr($substCharCode), $legalUtf8String); - return $cleanUtf8String; + return preg_replace($regexp, '�', $utf8String); } /** @@ -207,7 +209,7 @@ class CommandExecutor implements CommandExecutorInterface * @param string $composerBin * @param string $binary * - * @return false|string + * @return string|false */ protected function findBinaryLocal($composerBin, $binary) { @@ -223,7 +225,7 @@ class CommandExecutor implements CommandExecutorInterface /** * @param string $binary * - * @return false|string + * @return string|false */ protected function findBinaryGlobal($binary) { @@ -241,7 +243,7 @@ class CommandExecutor implements CommandExecutorInterface * * @param string $binary * - * @return false|string + * @return string|false */ protected function findBinarySystem($binary) { @@ -258,11 +260,11 @@ class CommandExecutor implements CommandExecutorInterface /** * Find a binary required by a plugin. * - * @param string $binary - * @param bool $quiet Returns null instead of throwing an exception. - * @param string $priorityPath + * @param array|string $binary + * @param bool $quiet Returns null instead of throwing an exception. + * @param string $priorityPath * - * @return null|string + * @return string|false * * @throws \Exception when no binary has been found and $quiet is false. */ @@ -317,7 +319,7 @@ class CommandExecutor implements CommandExecutorInterface } if ($quiet) { - return null; + return false; } throw new Exception(sprintf('Could not find %s', implode('/', $binary))); diff --git a/src/PHPCensor/Helper/CommandExecutorInterface.php b/src/PHPCensor/Helper/CommandExecutorInterface.php index 63a47b03..d66897c7 100644 --- a/src/PHPCensor/Helper/CommandExecutorInterface.php +++ b/src/PHPCensor/Helper/CommandExecutorInterface.php @@ -24,11 +24,11 @@ interface CommandExecutorInterface /** * Find a binary required by a plugin. * - * @param string $binary - * @param bool $quiet Returns null instead of throwing an exception. - * @param string $priorityPath + * @param array|string $binary + * @param bool $quiet Returns null instead of throwing an exception. + * @param string $priorityPath * - * @return null|string + * @return string|false * * @throws \Exception when no binary has been found and $quiet is false. */ diff --git a/src/PHPCensor/Plugin.php b/src/PHPCensor/Plugin.php index 0bb653bc..0a382dcb 100644 --- a/src/PHPCensor/Plugin.php +++ b/src/PHPCensor/Plugin.php @@ -55,10 +55,10 @@ abstract class Plugin /** * Find a binary required by a plugin. * - * @param string $binary - * @param boolean $quiet Returns null instead of throwing an exception. + * @param array|string $binary + * @param boolean $quiet Returns null instead of throwing an exception. * - * @return null|string + * @return string|false * * @throws \Exception when no binary has been found and $quiet is false. */ diff --git a/tests/PHPCensor/Helper/CommandExecutorTest.php b/tests/PHPCensor/Helper/CommandExecutorTest.php index 03e24d20..831d4599 100644 --- a/tests/PHPCensor/Helper/CommandExecutorTest.php +++ b/tests/PHPCensor/Helper/CommandExecutorTest.php @@ -80,6 +80,30 @@ EOD; public function testFindBinary_ReturnsNullWihQuietArgument() { $thisFileName = "WorldWidePeace"; - $this->assertNull($this->testedExecutor->findBinary($thisFileName, true)); + $this->assertFalse($this->testedExecutor->findBinary($thisFileName, true)); + } + + public function testReplaceIllegalCharacters() + { + $this->assertEquals( + \Normalizer::normalize("start � end"), + \Normalizer::normalize($this->testedExecutor->replaceIllegalCharacters( + "start \xf0\x9c\x83\x96 end" + )) + ); + + $this->assertEquals( + \Normalizer::normalize("start � end"), + \Normalizer::normalize($this->testedExecutor->replaceIllegalCharacters( + "start \xF0\x9C\x83\x96 end" + )) + ); + + $this->assertEquals( + \Normalizer::normalize("start 123_X08�_X00�_Xa�_5432 end"), + \Normalizer::normalize($this->testedExecutor->replaceIllegalCharacters( + "start 123_X08\x08_X00\x00_Xa4\xa4_5432 end" + )) + ); } }