From c0cd04ea66c45a523ec01cfb7bcedf8f4c1b021e Mon Sep 17 00:00:00 2001 From: jens1o Date: Tue, 7 Aug 2018 14:04:25 +0200 Subject: [PATCH] fixes https://github.com/alchemy-fr/BinaryDriver/issues/27 --- .travis.yml | 12 ++------ src/Alchemy/BinaryDriver/AbstractBinary.php | 29 +++++++++---------- .../BinaryDriver/BinaryDriverTestCase.php | 6 ++-- src/Alchemy/BinaryDriver/BinaryInterface.php | 9 +++--- src/Alchemy/BinaryDriver/Configuration.php | 13 +++++---- .../ConfigurationAwareInterface.php | 7 +++-- .../BinaryDriver/ConfigurationInterface.php | 13 +++++---- .../Exception/ExceptionInterface.php | 1 + .../Exception/ExecutableNotFoundException.php | 1 + .../Exception/ExecutionFailureException.php | 1 + .../Exception/InvalidArgumentException.php | 1 + .../BinaryDriver/Listeners/DebugListener.php | 13 +++++---- .../Listeners/ListenerInterface.php | 9 ++++-- .../BinaryDriver/Listeners/Listeners.php | 9 +++--- .../BinaryDriver/ProcessBuilderFactory.php | 21 +++++++------- .../ProcessBuilderFactoryAwareInterface.php | 5 ++-- .../ProcessBuilderFactoryInterface.php | 11 +++---- src/Alchemy/BinaryDriver/ProcessRunner.php | 24 ++++++++------- .../ProcessRunnerAwareInterface.php | 7 +++-- .../BinaryDriver/ProcessRunnerInterface.php | 9 +++--- .../Tests/BinaryDriver/AbstractBinaryTest.php | 2 +- .../AbstractProcessBuilderFactoryTest.php | 2 +- .../Tests/BinaryDriver/ConfigurationTest.php | 2 +- .../Listeners/DebugListenerTest.php | 2 +- .../BinaryDriver/Listeners/ListenersTest.php | 14 ++++++--- .../NONLTSProcessBuilderFactoryTest.php | 2 +- .../Tests/BinaryDriver/ProcessRunnerTest.php | 18 ++++++++---- tests/bootstrap.php | 2 +- 28 files changed, 137 insertions(+), 108 deletions(-) diff --git a/.travis.yml b/.travis.yml index 02e7ad4..46e9b71 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,21 +3,15 @@ language: php sudo: false php: - - 5.5 - - 5.6 - - 7.0 - 7.1 + - 7.2 + - nightly matrix: include: - - php: 5.5 + - php: 7.1 env: COMPOSER_OPTIONS="--prefer-lowest --prefer-stable" - - php: 7.0 - env: COMPOSER_REQUIRE="symfony/process:^2.0" - - php: 7.0 - env: COMPOSER_REQUIRE="symfony/process:^3.0" before_script: - composer self-update - - if [ -n "$COMPOSER_REQUIRE" ]; then composer require --no-update $COMPOSER_REQUIRE; fi - composer update $COMPOSER_OPTIONS diff --git a/src/Alchemy/BinaryDriver/AbstractBinary.php b/src/Alchemy/BinaryDriver/AbstractBinary.php index 7430ad8..b5372ca 100644 --- a/src/Alchemy/BinaryDriver/AbstractBinary.php +++ b/src/Alchemy/BinaryDriver/AbstractBinary.php @@ -1,4 +1,5 @@ listenersManager->register($listener, $this); @@ -58,7 +59,7 @@ public function listen(ListenerInterface $listener) /** * @inheritDoc */ - public function unlisten(ListenerInterface $listener) + public function unlisten(ListenerInterface $listener) : BinaryInterface { $this->listenersManager->unregister($listener, $this); @@ -68,17 +69,15 @@ public function unlisten(ListenerInterface $listener) /** * @inheritDoc */ - public function getConfiguration() + public function getConfiguration() : ConfigurationInterface { return $this->configuration; } /** * @inheritDoc - * - * @return BinaryInterface */ - public function setConfiguration(ConfigurationInterface $configuration) + public function setConfiguration(ConfigurationInterface $configuration) : ConfigurationAwareInterface { $this->configuration = $configuration; $this->applyProcessConfiguration(); @@ -89,7 +88,7 @@ public function setConfiguration(ConfigurationInterface $configuration) /** * @inheritDoc */ - public function getProcessBuilderFactory() + public function getProcessBuilderFactory() : ProcessBuilderFactoryInterface { return $this->factory; } @@ -97,9 +96,9 @@ public function getProcessBuilderFactory() /** * @inheritDoc * - * @return BinaryInterface + * @return ProcessBuilderFactoryAwareInterface */ - public function setProcessBuilderFactory(ProcessBuilderFactoryInterface $factory) + public function setProcessBuilderFactory(ProcessBuilderFactoryInterface $factory) : ProcessBuilderFactoryAwareInterface { $this->factory = $factory; $this->applyProcessConfiguration(); @@ -110,7 +109,7 @@ public function setProcessBuilderFactory(ProcessBuilderFactoryInterface $factory /** * @inheritDoc */ - public function getProcessRunner() + public function getProcessRunner() : ProcessRunnerInterface { return $this->processRunner; } @@ -118,17 +117,15 @@ public function getProcessRunner() /** * @inheritDoc */ - public function setProcessRunner(ProcessRunnerInterface $runner) + public function setProcessRunner(ProcessRunnerInterface $runner) : void { $this->processRunner = $runner; - - return $this; } /** * @inheritDoc */ - public function command($command, $bypassErrors = false, $listeners = null) + public function command($command, bool $bypassErrors = false, $listeners = null) : string { if (!is_array($command)) { $command = (array)$command; @@ -140,11 +137,11 @@ public function command($command, $bypassErrors = false, $listeners = null) /** * @inheritDoc */ - public static function load($binaries, LoggerInterface $logger = null, $configuration = []) + public static function load($binaries, ? LoggerInterface $logger = null, $configuration = []) : BinaryInterface { $finder = new ExecutableFinder(); $binary = null; - $binaries = is_array($binaries) ? $binaries : array($binaries); + $binaries = is_array($binaries) ? $binaries : (array)$binaries; foreach ($binaries as $candidate) { if (file_exists($candidate) && is_executable($candidate)) { diff --git a/src/Alchemy/BinaryDriver/BinaryDriverTestCase.php b/src/Alchemy/BinaryDriver/BinaryDriverTestCase.php index dcef80c..9b9b3d8 100644 --- a/src/Alchemy/BinaryDriver/BinaryDriverTestCase.php +++ b/src/Alchemy/BinaryDriver/BinaryDriverTestCase.php @@ -1,4 +1,5 @@ getMockBuilder(\Symfony\Component\Process\Process::class) ->disableOriginalConstructor() @@ -37,7 +39,7 @@ public function createProcessMock($runs = 1, $success = true, $commandLine = nul $builder = $process->expects($this->exactly($runs)) ->method('run'); - if (true === $callback) { + if (true === $enableCallback) { $builder->with($this->isInstanceOf('Closure')); } diff --git a/src/Alchemy/BinaryDriver/BinaryInterface.php b/src/Alchemy/BinaryDriver/BinaryInterface.php index ab2f170..e320a68 100644 --- a/src/Alchemy/BinaryDriver/BinaryInterface.php +++ b/src/Alchemy/BinaryDriver/BinaryInterface.php @@ -1,4 +1,5 @@ data[$key]) ? $this->data[$key] : $default; } @@ -39,7 +40,7 @@ public function get($key, $default = null) /** * @inheritDoc */ - public function set($key, $value) + public function set(string $key, $value) { $this->data[$key] = $value; @@ -49,15 +50,15 @@ public function set($key, $value) /** * @inheritDoc */ - public function has($key) + public function has(string $key) : bool { - return array_key_exists($key, $this->data); + return isset($this->data[$key]) || array_key_exists($key, $this->data); } /** * @inheritDoc */ - public function remove($key) + public function remove(string $key) { $value = $this->get($key); unset($this->data[$key]); @@ -68,7 +69,7 @@ public function remove($key) /** * @inheritDoc */ - public function all() + public function all() : array { return $this->data; } diff --git a/src/Alchemy/BinaryDriver/ConfigurationAwareInterface.php b/src/Alchemy/BinaryDriver/ConfigurationAwareInterface.php index 3b14f9e..bc4a730 100644 --- a/src/Alchemy/BinaryDriver/ConfigurationAwareInterface.php +++ b/src/Alchemy/BinaryDriver/ConfigurationAwareInterface.php @@ -1,4 +1,5 @@ prefixOut = $prefixOut; $this->prefixErr = $prefixErr; @@ -32,7 +33,7 @@ public function __construct($prefixOut = '[OUT] ', $prefixErr = '[ERROR] ', $eve /** * @inheritDoc */ - public function handle($type, $data) + public function handle(string $type, string $data) : void { if (Process::ERR === $type) { $this->emitLines($this->eventErr, $this->prefixErr, $data); @@ -44,15 +45,15 @@ public function handle($type, $data) /** * @inheritDoc */ - public function forwardedEvents() + public function forwardedEvents() : array { - return array_unique(array($this->eventErr, $this->eventOut)); + return array_unique([$this->eventErr, $this->eventOut]); } - private function emitLines($event, $prefix, $lines) + private function emitLines(string $event, string $prefix, string $lines) { foreach (explode("\n", $lines) as $line) { - $this->emit($event, array($prefix . $line)); + $this->emit($event, [$prefix . $line]); } } } diff --git a/src/Alchemy/BinaryDriver/Listeners/ListenerInterface.php b/src/Alchemy/BinaryDriver/Listeners/ListenerInterface.php index 920a6d5..761da0b 100644 --- a/src/Alchemy/BinaryDriver/Listeners/ListenerInterface.php +++ b/src/Alchemy/BinaryDriver/Listeners/ListenerInterface.php @@ -1,4 +1,5 @@ storage[$listener])) { throw new InvalidArgumentException('Listener is not registered.'); @@ -67,7 +68,7 @@ public function unregister(ListenerInterface $listener) return $this; } - private function forwardEvents($source, $target, array $events) + private function forwardEvents(ListenerInterface $source, EventEmitter $target, array $events) : array { $EElisteners = []; @@ -79,7 +80,7 @@ private function forwardEvents($source, $target, array $events) return $EElisteners; } - private function createListener($event, $target) + private function createListener(string $event, EventEmitter $target) : callable { return function () use ($event, $target) { $target->emit($event, func_get_args()); diff --git a/src/Alchemy/BinaryDriver/ProcessBuilderFactory.php b/src/Alchemy/BinaryDriver/ProcessBuilderFactory.php index 2ca8689..dd7a57f 100644 --- a/src/Alchemy/BinaryDriver/ProcessBuilderFactory.php +++ b/src/Alchemy/BinaryDriver/ProcessBuilderFactory.php @@ -1,4 +1,5 @@ detectEmulation(); @@ -91,17 +92,17 @@ public function setBuilder(ProcessBuilder $builder) } /** - * @inheritdoc + * @inheritDoc */ - public function getBinary() + public function getBinary() : string { return $this->binary; } /** - * @inheritdoc + * @inheritDoc */ - public function useBinary($binary) + public function useBinary(string $binary) : ProcessBuilderFactoryInterface { if (!is_executable($binary)) { throw new InvalidArgumentException(sprintf('`%s` is not an executable binary', $binary)); @@ -117,7 +118,7 @@ public function useBinary($binary) } /** - * @inheritdoc + * @inheritDoc */ public function setTimeout($timeout) { @@ -131,7 +132,7 @@ public function setTimeout($timeout) } /** - * @inheritdoc + * @inheritDoc */ public function getTimeout() { @@ -139,9 +140,9 @@ public function getTimeout() } /** - * @inheritdoc + * @inheritDoc */ - public function create($arguments = []) + public function create($arguments = []) : Process { if (null === $this->binary) { throw new InvalidArgumentException('No binary set'); diff --git a/src/Alchemy/BinaryDriver/ProcessBuilderFactoryAwareInterface.php b/src/Alchemy/BinaryDriver/ProcessBuilderFactoryAwareInterface.php index 1398bb4..ae48739 100644 --- a/src/Alchemy/BinaryDriver/ProcessBuilderFactoryAwareInterface.php +++ b/src/Alchemy/BinaryDriver/ProcessBuilderFactoryAwareInterface.php @@ -1,4 +1,5 @@ logger = $logger; $this->name = $name; @@ -33,20 +34,16 @@ public function __construct(LoggerInterface $logger, $name) /** * @inheritDoc - * - * @return ProcessRunner */ - public function setLogger(LoggerInterface $logger) + public function setLogger(LoggerInterface $logger) : void { $this->logger = $logger; - - return $this; } /** * @return LoggerInterface */ - public function getLogger() + public function getLogger() : LoggerInterface { return $this->logger; } @@ -54,7 +51,7 @@ public function getLogger() /** * @inheritDoc */ - public function run(Process $process, SplObjectStorage $listeners, $bypassErrors) + public function run(Process $process, SplObjectStorage $listeners, bool $bypassErrors = false) : string { $this->logger->info(sprintf( '%s running command %s', @@ -80,7 +77,7 @@ public function run(Process $process, SplObjectStorage $listeners, $bypassErrors $process->getErrorOutput() )); - return; + return ""; } else { $this->logger->info(sprintf('%s executed command successfully', $this->name)); @@ -97,7 +94,12 @@ private function buildCallback(SplObjectStorage $listeners) }; } - private function doExecutionFailure($command, \Exception $e = null) + /** + * @throws ExecutionFailureException + * + * @return void + */ + private function doExecutionFailure(? string $command, \Throwable $e = null) : void { $this->logger->error(sprintf( '%s failed to execute command %s', @@ -108,6 +110,6 @@ private function doExecutionFailure($command, \Exception $e = null) '%s failed to execute command %s', $this->name, $command - ), $e ? $e->getCode() : null, $e ? : null); + ), $e ? $e->getCode() : 0, $e); } } diff --git a/src/Alchemy/BinaryDriver/ProcessRunnerAwareInterface.php b/src/Alchemy/BinaryDriver/ProcessRunnerAwareInterface.php index 807c33e..eeb48ac 100644 --- a/src/Alchemy/BinaryDriver/ProcessRunnerAwareInterface.php +++ b/src/Alchemy/BinaryDriver/ProcessRunnerAwareInterface.php @@ -1,4 +1,5 @@ emit('received', array($type, $data)); } - public function forwardedEvents() + /** + * @inheritDoc + */ + public function forwardedEvents() : array { - return array('received'); + return ['received']; } } diff --git a/tests/Alchemy/Tests/BinaryDriver/NONLTSProcessBuilderFactoryTest.php b/tests/Alchemy/Tests/BinaryDriver/NONLTSProcessBuilderFactoryTest.php index fe5f343..c84ed0e 100644 --- a/tests/Alchemy/Tests/BinaryDriver/NONLTSProcessBuilderFactoryTest.php +++ b/tests/Alchemy/Tests/BinaryDriver/NONLTSProcessBuilderFactoryTest.php @@ -1,4 +1,4 @@ -expects($this->once()) ->method('info'); - $this->assertNull($runner->run($process, new \SplObjectStorage(), true)); + $this->assertSame('', $runner->run($process, new \SplObjectStorage(), true)); } public function testRunFailingProcessWithExceptionBypassingErrors() @@ -146,7 +146,7 @@ public function testRunFailingProcessWithExceptionBypassingErrors() ->expects($this->once()) ->method('info'); - $this->assertNull($runner->run($process, new \SplObjectStorage(), true)); + $this->assertSame('', $runner->run($process, new \SplObjectStorage(), true)); } public function testRunSuccessFullProcessWithHandlers() @@ -196,12 +196,18 @@ public function testRunSuccessFullProcessWithHandlers() class TestListener extends EventEmitter implements ListenerInterface { - public function handle($type, $data) + /** + * @inheritDoc + */ + public function handle(string $type, string $data) : void { - return $this->emit('received', array($type, $data)); + $this->emit('received', [$type, $data]); } - public function forwardedEvents() + /** + * @inheritDoc + */ + public function forwardedEvents() : array { return []; } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index b5e2aed..1909d18 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -1,4 +1,4 @@ -add('Alchemy\Tests', __DIR__);