Code style fixes + more tests for invalid UTF-8 log.

This commit is contained in:
Dmitry Khomutov 2018-02-04 12:44:41 +07:00
parent 3f6bec3a6b
commit 16755eadd4
No known key found for this signature in database
GPG key ID: EC19426474B37AAC
5 changed files with 56 additions and 30 deletions

View file

@ -308,11 +308,11 @@ class Builder implements LoggerAwareInterface
/** /**
* Find a binary required by a plugin. * Find a binary required by a plugin.
* *
* @param string $binary * @param array|string $binary
* @param bool $quiet Returns null instead of throwing an exception. * @param bool $quiet Returns null instead of throwing an exception.
* @param string $priorityPath * @param string $priorityPath
* *
* @return null|string * @return string|false
* *
* @throws \Exception when no binary has been found and $quiet is false. * @throws \Exception when no binary has been found and $quiet is false.
*/ */

View file

@ -36,6 +36,9 @@ class CommandExecutor implements CommandExecutorInterface
*/ */
protected $lastError; protected $lastError;
/**
* @var boolean
*/
public $logExecOutput = true; public $logExecOutput = true;
/** /**
@ -100,7 +103,6 @@ class CommandExecutor implements CommandExecutorInterface
$lastOutput = ''; $lastOutput = '';
$lastError = ''; $lastError = '';
if (is_resource($process)) { if (is_resource($process)) {
fclose($pipes[0]); fclose($pipes[0]);
@ -109,7 +111,7 @@ class CommandExecutor implements CommandExecutorInterface
$status = proc_close($process); $status = proc_close($process);
$lastOutput = $this->replaceIllegalCharacters($lastOutput); $lastOutput = $this->replaceIllegalCharacters($lastOutput);
$lastError = $this->replaceIllegalCharacters($lastError); $lastError = $this->replaceIllegalCharacters($lastError);
} }
$this->lastOutput = array_filter(explode(PHP_EOL, $lastOutput)); $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. * Reads from array of streams as data becomes available.
* *
* @param array $descriptors * @param array $descriptors
* *
* @return string[] data read from each descriptor * @return string[] data read from each descriptor
*/ */
@ -167,20 +169,20 @@ class CommandExecutor implements CommandExecutorInterface
return $outputs; 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]' . $regexp = '/[\x00-\x08\x10\x0B\x0C\x0E-\x19\x7F]' .
'|[\x00-\x7F][\x80-\xBF]+' . '|[\x00-\x7F][\x80-\xBF]+' .
'|([\xC0\xC1]|[\xF0-\xFF])[\x80-\xBF]*' . '|([\xC0\xC1]|[\xF0-\xFF])[\x80-\xBF]*' .
'|[\xC2-\xDF]((?![\x80-\xBF])|[\x80-\xBF]{2,})' . '|[\xC2-\xDF]((?![\x80-\xBF])|[\x80-\xBF]{2,})' .
'|[\xE0-\xEF](([\x80-\xBF](?![\x80-\xBF]))|(?![\x80-\xBF]{2})|[\x80-\xBF]{3,})/S'; '|[\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, '<27>', $utf8String);
} }
/** /**
@ -207,7 +209,7 @@ class CommandExecutor implements CommandExecutorInterface
* @param string $composerBin * @param string $composerBin
* @param string $binary * @param string $binary
* *
* @return false|string * @return string|false
*/ */
protected function findBinaryLocal($composerBin, $binary) protected function findBinaryLocal($composerBin, $binary)
{ {
@ -223,7 +225,7 @@ class CommandExecutor implements CommandExecutorInterface
/** /**
* @param string $binary * @param string $binary
* *
* @return false|string * @return string|false
*/ */
protected function findBinaryGlobal($binary) protected function findBinaryGlobal($binary)
{ {
@ -241,7 +243,7 @@ class CommandExecutor implements CommandExecutorInterface
* *
* @param string $binary * @param string $binary
* *
* @return false|string * @return string|false
*/ */
protected function findBinarySystem($binary) protected function findBinarySystem($binary)
{ {
@ -258,11 +260,11 @@ class CommandExecutor implements CommandExecutorInterface
/** /**
* Find a binary required by a plugin. * Find a binary required by a plugin.
* *
* @param string $binary * @param array|string $binary
* @param bool $quiet Returns null instead of throwing an exception. * @param bool $quiet Returns null instead of throwing an exception.
* @param string $priorityPath * @param string $priorityPath
* *
* @return null|string * @return string|false
* *
* @throws \Exception when no binary has been found and $quiet is false. * @throws \Exception when no binary has been found and $quiet is false.
*/ */
@ -317,7 +319,7 @@ class CommandExecutor implements CommandExecutorInterface
} }
if ($quiet) { if ($quiet) {
return null; return false;
} }
throw new Exception(sprintf('Could not find %s', implode('/', $binary))); throw new Exception(sprintf('Could not find %s', implode('/', $binary)));

View file

@ -24,11 +24,11 @@ interface CommandExecutorInterface
/** /**
* Find a binary required by a plugin. * Find a binary required by a plugin.
* *
* @param string $binary * @param array|string $binary
* @param bool $quiet Returns null instead of throwing an exception. * @param bool $quiet Returns null instead of throwing an exception.
* @param string $priorityPath * @param string $priorityPath
* *
* @return null|string * @return string|false
* *
* @throws \Exception when no binary has been found and $quiet is false. * @throws \Exception when no binary has been found and $quiet is false.
*/ */

View file

@ -55,10 +55,10 @@ abstract class Plugin
/** /**
* Find a binary required by a plugin. * Find a binary required by a plugin.
* *
* @param string $binary * @param array|string $binary
* @param boolean $quiet Returns null instead of throwing an exception. * @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. * @throws \Exception when no binary has been found and $quiet is false.
*/ */

View file

@ -80,6 +80,30 @@ EOD;
public function testFindBinary_ReturnsNullWihQuietArgument() public function testFindBinary_ReturnsNullWihQuietArgument()
{ {
$thisFileName = "WorldWidePeace"; $thisFileName = "WorldWidePeace";
$this->assertNull($this->testedExecutor->findBinary($thisFileName, true)); $this->assertFalse($this->testedExecutor->findBinary($thisFileName, true));
}
public function testReplaceIllegalCharacters()
{
$this->assertEquals(
\Normalizer::normalize("start <20> end"),
\Normalizer::normalize($this->testedExecutor->replaceIllegalCharacters(
"start \xf0\x9c\x83\x96 end"
))
);
$this->assertEquals(
\Normalizer::normalize("start <20> end"),
\Normalizer::normalize($this->testedExecutor->replaceIllegalCharacters(
"start \xF0\x9C\x83\x96 end"
))
);
$this->assertEquals(
\Normalizer::normalize("start 123_X08<30>_X00<30>_Xa<58>_5432 end"),
\Normalizer::normalize($this->testedExecutor->replaceIllegalCharacters(
"start 123_X08\x08_X00\x00_Xa4\xa4_5432 end"
))
);
} }
} }