From a556897a8242ec356328475cfaab055bba2235fc Mon Sep 17 00:00:00 2001 From: NanoSector Date: Tue, 14 Jul 2020 15:57:25 +0200 Subject: [PATCH 1/8] Update gitignore --- .gitignore | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 82 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index e61b1fb..1414130 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,83 @@ +# Created by .ignore support plugin (hsz.mobi) +### JetBrains template +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/**/usage.statistics.xml +.idea/**/dictionaries +.idea/**/shelf + +# Generated files +.idea/**/contentModel.xml + +# Sensitive or high-churn files +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml +.idea/**/dbnavigator.xml + +# Gradle +.idea/**/gradle.xml +.idea/**/libraries + +# Gradle and Maven with auto-import +# When using Gradle or Maven with auto-import, you should exclude module files, +# since they will be recreated, and may cause churn. Uncomment if using +# auto-import. +# .idea/artifacts +# .idea/compiler.xml +# .idea/jarRepositories.xml +# .idea/modules.xml +# .idea/*.iml +# .idea/modules +# *.iml +# *.ipr + +# CMake +cmake-build-*/ + +# Mongo Explorer plugin +.idea/**/mongoSettings.xml + +# File-based project format +*.iws + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +# Editor-based Rest Client +.idea/httpRequests + +# Android studio 3.1+ serialized cache file +.idea/caches/build_file_checksums.ser + +### Composer template composer.phar -vendor/ -.idea/ \ No newline at end of file +/vendor/ + +# Commit your application's lock file https://getcomposer.org/doc/01-basic-usage.md#commit-your-composer-lock-file-to-version-control +# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file +# composer.lock + +.phpunit.result.cache From ef9fe388ead32a15478e925ebc6f7872464a9f29 Mon Sep 17 00:00:00 2001 From: NanoSector Date: Tue, 14 Jul 2020 16:02:21 +0200 Subject: [PATCH 2/8] Update composer packages --- composer.json | 2 +- composer.lock | 60 ++++++++++++++++++++++++++++----------------------- 2 files changed, 34 insertions(+), 28 deletions(-) diff --git a/composer.json b/composer.json index 9b193ab..b94f81d 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,7 @@ } }, "require": { - "php": ">=7.2.0", + "php": ">=7.2", "yoshi2889/validation-closures": "^0.2", "yoshi2889/collections": "^0.1" }, diff --git a/composer.lock b/composer.lock index 9d516a3..9b03b45 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "aeea27b7bbca1eb8fb26c2dfb27a6e6b", + "content-hash": "0e97f4c3abfb89181427173025deea00", "packages": [ { "name": "evenement/evenement", @@ -512,33 +512,33 @@ }, { "name": "phpspec/prophecy", - "version": "v1.10.3", + "version": "1.11.1", "source": { "type": "git", "url": "https://github.com/phpspec/prophecy.git", - "reference": "451c3cd1418cf640de218914901e51b064abb093" + "reference": "b20034be5efcdab4fb60ca3a29cba2949aead160" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpspec/prophecy/zipball/451c3cd1418cf640de218914901e51b064abb093", - "reference": "451c3cd1418cf640de218914901e51b064abb093", + "url": "https://api.github.com/repos/phpspec/prophecy/zipball/b20034be5efcdab4fb60ca3a29cba2949aead160", + "reference": "b20034be5efcdab4fb60ca3a29cba2949aead160", "shasum": "" }, "require": { - "doctrine/instantiator": "^1.0.2", - "php": "^5.3|^7.0", - "phpdocumentor/reflection-docblock": "^2.0|^3.0.2|^4.0|^5.0", - "sebastian/comparator": "^1.2.3|^2.0|^3.0|^4.0", - "sebastian/recursion-context": "^1.0|^2.0|^3.0|^4.0" + "doctrine/instantiator": "^1.2", + "php": "^7.2", + "phpdocumentor/reflection-docblock": "^5.0", + "sebastian/comparator": "^3.0 || ^4.0", + "sebastian/recursion-context": "^3.0 || ^4.0" }, "require-dev": { - "phpspec/phpspec": "^2.5 || ^3.2", - "phpunit/phpunit": "^4.8.35 || ^5.7 || ^6.5 || ^7.1" + "phpspec/phpspec": "^6.0", + "phpunit/phpunit": "^8.0" }, "type": "library", "extra": { "branch-alias": { - "dev-master": "1.10.x-dev" + "dev-master": "1.11.x-dev" } }, "autoload": { @@ -571,7 +571,7 @@ "spy", "stub" ], - "time": "2020-03-05T15:02:03+00:00" + "time": "2020-07-08T12:44:21+00:00" }, { "name": "phpunit/php-code-coverage", @@ -1662,23 +1662,23 @@ }, { "name": "theseer/tokenizer", - "version": "1.1.3", + "version": "1.2.0", "source": { "type": "git", "url": "https://github.com/theseer/tokenizer.git", - "reference": "11336f6f84e16a720dae9d8e6ed5019efa85a0f9" + "reference": "75a63c33a8577608444246075ea0af0d052e452a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/theseer/tokenizer/zipball/11336f6f84e16a720dae9d8e6ed5019efa85a0f9", - "reference": "11336f6f84e16a720dae9d8e6ed5019efa85a0f9", + "url": "https://api.github.com/repos/theseer/tokenizer/zipball/75a63c33a8577608444246075ea0af0d052e452a", + "reference": "75a63c33a8577608444246075ea0af0d052e452a", "shasum": "" }, "require": { "ext-dom": "*", "ext-tokenizer": "*", "ext-xmlwriter": "*", - "php": "^7.0" + "php": "^7.2 || ^8.0" }, "type": "library", "autoload": { @@ -1698,24 +1698,30 @@ } ], "description": "A small library for converting tokenized PHP source code into XML and potentially other formats", - "time": "2019-06-13T22:48:21+00:00" + "funding": [ + { + "url": "https://github.com/theseer", + "type": "github" + } + ], + "time": "2020-07-12T23:59:07+00:00" }, { "name": "webmozart/assert", - "version": "1.9.0", + "version": "1.9.1", "source": { "type": "git", "url": "https://github.com/webmozart/assert.git", - "reference": "9dc4f203e36f2b486149058bade43c851dd97451" + "reference": "bafc69caeb4d49c39fd0779086c03a3738cbb389" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/webmozart/assert/zipball/9dc4f203e36f2b486149058bade43c851dd97451", - "reference": "9dc4f203e36f2b486149058bade43c851dd97451", + "url": "https://api.github.com/repos/webmozart/assert/zipball/bafc69caeb4d49c39fd0779086c03a3738cbb389", + "reference": "bafc69caeb4d49c39fd0779086c03a3738cbb389", "shasum": "" }, "require": { - "php": "^5.3.3 || ^7.0", + "php": "^5.3.3 || ^7.0 || ^8.0", "symfony/polyfill-ctype": "^1.8" }, "conflict": { @@ -1747,7 +1753,7 @@ "check", "validate" ], - "time": "2020-06-16T10:16:42+00:00" + "time": "2020-07-08T17:02:28+00:00" } ], "aliases": [], @@ -1756,7 +1762,7 @@ "prefer-stable": false, "prefer-lowest": false, "platform": { - "php": ">=7.2.0" + "php": ">=7.2" }, "platform-dev": [], "platform-overrides": { From 120fd9e055104d94ae47d247bc12fb809c22c6d5 Mon Sep 17 00:00:00 2001 From: NanoSector Date: Tue, 14 Jul 2020 16:03:03 +0200 Subject: [PATCH 3/8] Add IDEA files --- .idea/command-parser.iml | 47 +++++++ .idea/highlightedFiles.xml | 15 +++ .idea/inspectionProfiles/Project_Default.xml | 131 +++++++++++++++++++ .idea/modules.xml | 8 ++ .idea/php-test-framework.xml | 14 ++ .idea/php.xml | 52 ++++++++ .idea/phpunit.xml | 10 ++ .idea/vcs.xml | 6 + 8 files changed, 283 insertions(+) create mode 100644 .idea/command-parser.iml create mode 100644 .idea/highlightedFiles.xml create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/php-test-framework.xml create mode 100644 .idea/php.xml create mode 100644 .idea/phpunit.xml create mode 100644 .idea/vcs.xml diff --git a/.idea/command-parser.iml b/.idea/command-parser.iml new file mode 100644 index 0000000..8ded7f8 --- /dev/null +++ b/.idea/command-parser.iml @@ -0,0 +1,47 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/highlightedFiles.xml b/.idea/highlightedFiles.xml new file mode 100644 index 0000000..814305f --- /dev/null +++ b/.idea/highlightedFiles.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..db55379 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,131 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..2d3c37e --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/php-test-framework.xml b/.idea/php-test-framework.xml new file mode 100644 index 0000000..a8ec527 --- /dev/null +++ b/.idea/php-test-framework.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/php.xml b/.idea/php.xml new file mode 100644 index 0000000..4b5cc68 --- /dev/null +++ b/.idea/php.xml @@ -0,0 +1,52 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/phpunit.xml b/.idea/phpunit.xml new file mode 100644 index 0000000..4f8104c --- /dev/null +++ b/.idea/phpunit.xml @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file From ca70bd2c58ff53509d53a510cc6c27f490bc9ae2 Mon Sep 17 00:00:00 2001 From: NanoSector Date: Tue, 14 Jul 2020 16:05:30 +0200 Subject: [PATCH 4/8] Run code style utilities --- src/Command.php | 13 ++-- src/CommandParser.php | 23 ++++-- src/CommandProcessor.php | 9 ++- src/Exceptions/CommandNotFoundException.php | 6 +- .../InvalidParameterCountException.php | 4 +- .../NoApplicableStrategiesException.php | 6 +- src/Exceptions/ParseException.php | 6 +- src/Exceptions/ValidationException.php | 6 +- src/ParameterStrategy.php | 8 +- .../ConvertibleParameterInterface.php | 3 +- src/Parameters/NumericParameter.php | 7 +- src/Parameters/Parameter.php | 7 +- src/Parameters/ParameterInterface.php | 4 +- src/Parameters/PredefinedStringParameter.php | 3 +- src/Parameters/StringParameter.php | 3 +- src/ParsedCommand.php | 6 +- src/ProcessedCommand.php | 14 ++-- tests/CommandParserTest.php | 34 +++++---- tests/CommandProcessorTest.php | 37 +++++---- tests/CommandTest.php | 42 ++++++----- tests/MockConvertibleParameter.php | 3 +- tests/MockRejectAllParameter.php | 3 +- tests/NumericParameterTest.php | 5 +- tests/ParameterStrategyTest.php | 75 ++++++++++--------- tests/ParsedCommandTest.php | 7 +- tests/PredefinedStringParameterTest.php | 9 ++- tests/ProcessedCommandTest.php | 11 ++- 27 files changed, 212 insertions(+), 142 deletions(-) diff --git a/src/Command.php b/src/Command.php index 359de10..16852c2 100644 --- a/src/Command.php +++ b/src/Command.php @@ -1,4 +1,5 @@ callback = $callback; } @@ -65,12 +68,12 @@ public function getParameterStrategies(): array /** * @param ParameterStrategy[] $parameterStrategies */ - public function setParameterStrategies(array $parameterStrategies) + public function setParameterStrategies(array $parameterStrategies): void { if (!Utils::validateArray(Types::instanceof(ParameterStrategy::class), $parameterStrategies)) { - throw new \InvalidArgumentException('Invalid array passed'); + throw new InvalidArgumentException('Invalid array passed'); } $this->parameterStrategies = $parameterStrategies; } -} \ No newline at end of file +} diff --git a/src/CommandParser.php b/src/CommandParser.php index 247150a..8d96e6b 100644 --- a/src/CommandParser.php +++ b/src/CommandParser.php @@ -31,8 +31,9 @@ public static function findApplicableStrategy(Command $commandObject, array $par foreach ($parameterStrategies as $parameterStrategy) { $result = $parameterStrategy->validateParameterArray($parameters); - if ($result) + if ($result) { return $parameterStrategy; + } } throw new NoApplicableStrategiesException(); @@ -60,12 +61,18 @@ public static function parseFromString(string $string, string $prefix = '!'): Pa $command = substr($firstPart, strlen($prefix)); // Remove empty elements and excessive spaces. - $parameters = array_values(array_map('trim', array_filter($messageParts, function ($parameter) { - return !preg_match('/^$|\s/', $parameter); - }))); + $parameters = array_values( + array_map( + 'trim', + array_filter( + $messageParts, + static function ($parameter) { + return !preg_match('/^$|\s/', $parameter); + } + ) + ) + ); - $parsedCommand = new ParsedCommand($command, $parameters); - - return $parsedCommand; + return new ParsedCommand($command, $parameters); } -} \ No newline at end of file +} diff --git a/src/CommandProcessor.php b/src/CommandProcessor.php index 78455b8..5a76ed4 100644 --- a/src/CommandProcessor.php +++ b/src/CommandProcessor.php @@ -10,6 +10,7 @@ namespace WildPHP\Commands; use ValidationClosures\Types; +use WildPHP\Commands\Exceptions\CommandNotFoundException; use Yoshi2889\Collections\Collection; class CommandProcessor @@ -17,7 +18,7 @@ class CommandProcessor /** * @var Collection */ - protected $commandCollection = null; + protected $commandCollection; /** * CommandProcessor constructor. @@ -63,7 +64,7 @@ public function registerCommand(string $command, Command $commandObject): bool * @param string $command * * @return Command - * @throws Exceptions\CommandNotFoundException + * @throws CommandNotFoundException */ public function findCommand(string $command): Command { @@ -110,8 +111,8 @@ public function getCommandCollection(): Collection /** * @param Collection $commandCollection */ - public function setCommandCollection(Collection $commandCollection) + public function setCommandCollection(Collection $commandCollection): void { $this->commandCollection = $commandCollection; } -} \ No newline at end of file +} diff --git a/src/Exceptions/CommandNotFoundException.php b/src/Exceptions/CommandNotFoundException.php index 2ec144d..654dfe8 100644 --- a/src/Exceptions/CommandNotFoundException.php +++ b/src/Exceptions/CommandNotFoundException.php @@ -1,4 +1,5 @@ = 0 && $minimumParameters > $maximumParameters) { - throw new \InvalidArgumentException('Invalid parameter range (minimum cannot be larger than maximum)'); + throw new InvalidArgumentException('Invalid parameter range (minimum cannot be larger than maximum)'); } parent::__construct(Types::instanceof(ParameterInterface::class), $initialValues); @@ -133,7 +135,7 @@ public function validateParameterCount(array $args): bool public function convertParameter(string $parameterName, string $parameterValue) { if (!$this->offsetExists($parameterName)) { - throw new \InvalidArgumentException('Parameter name does not exist'); + throw new InvalidArgumentException('Parameter name does not exist'); } if (!($this[$parameterName] instanceof ConvertibleParameterInterface)) { @@ -208,4 +210,4 @@ public static function implodeLeftoverParameters(array $arguments, int $offset): $array2 = [implode(' ', array_slice($arguments, $offset))]; return array_merge($array1, $array2); } -} \ No newline at end of file +} diff --git a/src/Parameters/ConvertibleParameterInterface.php b/src/Parameters/ConvertibleParameterInterface.php index 39bba53..5181e0c 100644 --- a/src/Parameters/ConvertibleParameterInterface.php +++ b/src/Parameters/ConvertibleParameterInterface.php @@ -9,7 +9,6 @@ namespace WildPHP\Commands\Parameters; - interface ConvertibleParameterInterface extends ParameterInterface { /** @@ -17,4 +16,4 @@ interface ConvertibleParameterInterface extends ParameterInterface * @return mixed Output may be unpredictable. */ public function convert(string $input); -} \ No newline at end of file +} diff --git a/src/Parameters/NumericParameter.php b/src/Parameters/NumericParameter.php index 7ef349d..a423b52 100644 --- a/src/Parameters/NumericParameter.php +++ b/src/Parameters/NumericParameter.php @@ -1,4 +1,5 @@ validationClosure = $validationClosure; } @@ -32,4 +35,4 @@ public function validate(string $input): bool { return ($this->validationClosure)($input); } -} \ No newline at end of file +} diff --git a/src/Parameters/ParameterInterface.php b/src/Parameters/ParameterInterface.php index 76f3127..341404f 100644 --- a/src/Parameters/ParameterInterface.php +++ b/src/Parameters/ParameterInterface.php @@ -1,4 +1,5 @@ arguments = $arguments; } - - - -} \ No newline at end of file +} diff --git a/src/ProcessedCommand.php b/src/ProcessedCommand.php index 8bbc13a..7c51dcd 100644 --- a/src/ProcessedCommand.php +++ b/src/ProcessedCommand.php @@ -9,13 +9,12 @@ namespace WildPHP\Commands; - class ProcessedCommand extends ParsedCommand { /** * @var ParameterStrategy */ - protected $applicableStrategy = null; + protected $applicableStrategy; /** * @var array @@ -35,8 +34,13 @@ class ProcessedCommand extends ParsedCommand * @param array $convertedParameters * @param callable $callback */ - public function __construct(string $command, array $arguments, ParameterStrategy $applicableStrategy, array $convertedParameters, callable $callback) - { + public function __construct( + string $command, + array $arguments, + ParameterStrategy $applicableStrategy, + array $convertedParameters, + callable $callback + ) { parent::__construct($command, $arguments); $this->convertedParameters = $convertedParameters; $this->applicableStrategy = $applicableStrategy; @@ -66,4 +70,4 @@ public function getCallback(): callable { return $this->callback; } -} \ No newline at end of file +} diff --git a/tests/CommandParserTest.php b/tests/CommandParserTest.php index 55d99a5..b31a1df 100644 --- a/tests/CommandParserTest.php +++ b/tests/CommandParserTest.php @@ -1,4 +1,5 @@ assertSame($parameterStrategy, CommandParser::findApplicableStrategy($command, ['1'])); + self::assertSame($parameterStrategy, CommandParser::findApplicableStrategy($command, ['1'])); - $parameterStrategy = new \WildPHP\Commands\ParameterStrategy(1, 1, [ - new \WildPHP\Tests\MockRejectAllParameter() + $parameterStrategy = new ParameterStrategy(1, 1, [ + new MockRejectAllParameter() ]); - $command = new \WildPHP\Commands\Command([$this, 'foo'], [$parameterStrategy]); + $command = new Command([$this, 'foo'], [$parameterStrategy]); - $this->expectException(\WildPHP\Commands\Exceptions\NoApplicableStrategiesException::class); + $this->expectException(NoApplicableStrategiesException::class); CommandParser::findApplicableStrategy($command, ['test']); } @@ -39,16 +47,16 @@ public function testParseFromString() { $string = '!test param1'; - $expected = new \WildPHP\Commands\ParsedCommand('test', ['param1']); + $expected = new ParsedCommand('test', ['param1']); - $this->assertEquals($expected, CommandParser::parseFromString($string, '!')); + self::assertEquals($expected, CommandParser::parseFromString($string, '!')); } public function testParseFromStringFirstPartIsPrefix() { $string = '! test param1'; - $this->expectException(\WildPHP\Commands\Exceptions\ParseException::class); + $this->expectException(ParseException::class); CommandParser::parseFromString($string, '!'); } @@ -56,7 +64,7 @@ public function testParseFromStringNoPrefix() { $string = 'test param1'; - $this->expectException(\WildPHP\Commands\Exceptions\ParseException::class); + $this->expectException(ParseException::class); CommandParser::parseFromString($string, '!'); } } diff --git a/tests/CommandProcessorTest.php b/tests/CommandProcessorTest.php index dfacf10..91dcecb 100644 --- a/tests/CommandProcessorTest.php +++ b/tests/CommandProcessorTest.php @@ -1,4 +1,5 @@ assertTrue($commandProcessor->registerCommand('test', $command)); - $this->assertFalse($commandProcessor->registerCommand('test', $command)); + self::assertTrue($commandProcessor->registerCommand('test', $command)); + self::assertFalse($commandProcessor->registerCommand('test', $command)); - $this->assertSame($command, $commandProcessor->findCommand('test')); + self::assertSame($command, $commandProcessor->findCommand('test')); } public function testInvalidFindCommand() { - $command = new \WildPHP\Commands\Command([$this, 'foo'], new \WildPHP\Commands\ParameterStrategy()); + $command = new Command([$this, 'foo'], new ParameterStrategy()); $commandProcessor = new CommandProcessor(); $commandProcessor->registerCommand('test', $command); - $this->expectException(\WildPHP\Commands\Exceptions\CommandNotFoundException::class); + $this->expectException(CommandNotFoundException::class); $commandProcessor->findCommand('ing'); } public function testProcess() { - $parameterStrategy = new \WildPHP\Commands\ParameterStrategy(1, 1, [ - new \WildPHP\Commands\Parameters\NumericParameter() + $parameterStrategy = new ParameterStrategy(1, 1, [ + new NumericParameter() ]); - $command = new \WildPHP\Commands\Command([$this, 'foo'], [$parameterStrategy]); + $command = new Command([$this, 'foo'], [$parameterStrategy]); - $expectedProcessedCommand = new \WildPHP\Commands\ProcessedCommand( + $expectedProcessedCommand = new ProcessedCommand( 'test', ['1'], $parameterStrategy, @@ -56,8 +65,8 @@ public function testProcess() $commandProcessor = new CommandProcessor(); $commandProcessor->registerCommand('test', $command); - $parsedCommand = new \WildPHP\Commands\ParsedCommand('test', ['1']); + $parsedCommand = new ParsedCommand('test', ['1']); - $this->assertEquals($expectedProcessedCommand, $commandProcessor->process($parsedCommand)); + self::assertEquals($expectedProcessedCommand, $commandProcessor->process($parsedCommand)); } } diff --git a/tests/CommandTest.php b/tests/CommandTest.php index 179e611..e852f16 100644 --- a/tests/CommandTest.php +++ b/tests/CommandTest.php @@ -1,4 +1,5 @@ setCallback([$this, 'bar']); - $this->assertEquals([$this, 'bar'], $command->getCallback()); + self::assertEquals([$this, 'bar'], $command->getCallback()); } public function testGetParameterStrategies() { $parameterStrategies = [ - new \WildPHP\Commands\ParameterStrategy(), - new \WildPHP\Commands\ParameterStrategy(), - new \WildPHP\Commands\ParameterStrategy(), - new \WildPHP\Commands\ParameterStrategy() + new ParameterStrategy(), + new ParameterStrategy(), + new ParameterStrategy(), + new ParameterStrategy() ]; $command = new Command([$this, 'foo'], $parameterStrategies); - $this->assertSame($parameterStrategies, $command->getParameterStrategies()); + self::assertSame($parameterStrategies, $command->getParameterStrategies()); } public function testGetCallback() { - $parameterStrategy = new \WildPHP\Commands\ParameterStrategy(); + $parameterStrategy = new ParameterStrategy(); $command = new Command([$this, 'foo'], $parameterStrategy); - $this->assertEquals([$this, 'foo'], $command->getCallback()); + self::assertEquals([$this, 'foo'], $command->getCallback()); } public function testSetParameterStrategies() { $parameterStrategies = [ - new \WildPHP\Commands\ParameterStrategy(), - new \WildPHP\Commands\ParameterStrategy(), - new \WildPHP\Commands\ParameterStrategy(), - new \WildPHP\Commands\ParameterStrategy() + new ParameterStrategy(), + new ParameterStrategy(), + new ParameterStrategy(), + new ParameterStrategy() ]; - $command = new Command([$this, 'foo'], new \WildPHP\Commands\ParameterStrategy()); + $command = new Command([$this, 'foo'], new ParameterStrategy()); $command->setParameterStrategies($parameterStrategies); - $this->assertSame($parameterStrategies, $command->getParameterStrategies()); + self::assertSame($parameterStrategies, $command->getParameterStrategies()); - $this->expectException(\InvalidArgumentException::class); - $command->setParameterStrategies([new \WildPHP\Commands\Parameters\NumericParameter()]); + $this->expectException(InvalidArgumentException::class); + $command->setParameterStrategies([new NumericParameter()]); } } diff --git a/tests/MockConvertibleParameter.php b/tests/MockConvertibleParameter.php index e5ce4f4..7ac5223 100644 --- a/tests/MockConvertibleParameter.php +++ b/tests/MockConvertibleParameter.php @@ -1,4 +1,5 @@ assertSame(3, $numericParameter->convert('3')); + self::assertSame(3, $numericParameter->convert('3')); } } diff --git a/tests/ParameterStrategyTest.php b/tests/ParameterStrategyTest.php index add6e5e..8d76f1d 100644 --- a/tests/ParameterStrategyTest.php +++ b/tests/ParameterStrategyTest.php @@ -1,4 +1,5 @@ assertTrue($parameterStrategy->validateParameterCount([])); // 0 - $this->assertTrue($parameterStrategy->validateParameterCount(['test'])); // 1 - $this->assertFalse($parameterStrategy->validateParameterCount(['test', 'ing'])); // 2 + self::assertTrue($parameterStrategy->validateParameterCount([])); // 0 + self::assertTrue($parameterStrategy->validateParameterCount(['test'])); // 1 + self::assertFalse($parameterStrategy->validateParameterCount(['test', 'ing'])); // 2 } public function testImplodeLeftoverParameters() @@ -29,16 +36,16 @@ public function testImplodeLeftoverParameters() $parameterStrategy = new ParameterStrategy(0, 1, [], true); $parameters = ['test', 'ing', 'something', 'large']; - $this->assertTrue($parameterStrategy->validateParameterCount($parameters)); + self::assertTrue($parameterStrategy->validateParameterCount($parameters)); - $this->assertSame( + self::assertSame( [ 'test ing something large', ], ParameterStrategy::implodeLeftoverParameters($parameters, 0) ); - $this->assertSame( + self::assertSame( [ 'test', 'ing something large', @@ -46,7 +53,7 @@ public function testImplodeLeftoverParameters() ParameterStrategy::implodeLeftoverParameters($parameters, 1) ); - $this->assertSame( + self::assertSame( [ 'test', 'ing', @@ -59,32 +66,32 @@ public function testImplodeLeftoverParameters() public function testValidateParameter() { $parameterStrategy = new ParameterStrategy(1, 1, [ - 'test' => new \WildPHP\Commands\Parameters\NumericParameter() + 'test' => new NumericParameter() ]); - $this->assertTrue($parameterStrategy->validateParameter('test', 1)); - $this->assertFalse($parameterStrategy->validateParameter('test', 'ing')); + self::assertTrue($parameterStrategy->validateParameter('test', 1)); + self::assertFalse($parameterStrategy->validateParameter('test', 'ing')); } public function testRemapNumericParameterIndexes() { $parameterStrategy = new ParameterStrategy(1, 1, [ - 'test' => new \WildPHP\Commands\Parameters\NumericParameter(), - 'ing' => new \WildPHP\Commands\Parameters\NumericParameter() + 'test' => new NumericParameter(), + 'ing' => new NumericParameter() ]); $parameters = [1, 2]; $expected = ['test' => 1, 'ing' => 2]; - $this->assertEquals($expected, $parameterStrategy->remapNumericParameterIndexes($parameters)); + self::assertEquals($expected, $parameterStrategy->remapNumericParameterIndexes($parameters)); $parameters = [1, 'foo' => 2]; $expected = ['test' => 1, 'foo' => 2]; - $this->assertEquals($expected, $parameterStrategy->remapNumericParameterIndexes($parameters)); + self::assertEquals($expected, $parameterStrategy->remapNumericParameterIndexes($parameters)); } public function testConvertParameter() { - $parameterStrategy = new \WildPHP\Commands\ParameterStrategy(1, 4, [ + $parameterStrategy = new ParameterStrategy(1, 4, [ new MockConvertibleParameter(), new MockConvertibleParameter(), new MockConvertibleParameter(), @@ -93,51 +100,51 @@ public function testConvertParameter() $parameters = ['test', 'test', 'test', 'test']; - $this->assertEquals( + self::assertEquals( 'ing', $parameterStrategy->convertParameter(1, 'test') ); - $this->assertEquals( + self::assertEquals( ['ing', 'ing', 'ing', 'ing'], $parameterStrategy->convertParameterArray($parameters) ); - $parameterStrategy = new \WildPHP\Commands\ParameterStrategy(1, 1, [ - 'test' => new \WildPHP\Commands\Parameters\StringParameter() + $parameterStrategy = new ParameterStrategy(1, 1, [ + 'test' => new StringParameter() ]); - $this->assertEquals( + self::assertEquals( ['test' => 'test'], $parameterStrategy->convertParameterArray(['test' => 'test']) ); - $this->assertEquals( + self::assertEquals( 'test', $parameterStrategy->convertParameter('test', 'test') ); - $this->expectException(\InvalidArgumentException::class); + $this->expectException(InvalidArgumentException::class); $parameterStrategy->convertParameter('testing', 'ing'); } public function testValidateParameterArray() { $parameterStrategy = new ParameterStrategy(3, 3, [ - 'test' => new \WildPHP\Commands\Parameters\NumericParameter(), - 'test2' => new \WildPHP\Commands\Parameters\NumericParameter(), - 'test3' => new \WildPHP\Commands\Parameters\StringParameter() + 'test' => new NumericParameter(), + 'test2' => new NumericParameter(), + 'test3' => new StringParameter() ]); - $this->assertTrue($parameterStrategy->validateParameterArray([1, 2, 3])); - $this->assertTrue($parameterStrategy->validateParameterArray([1, 2, 'test'])); - $this->assertFalse($parameterStrategy->validateParameterArray(['test', 2, 3])); + self::assertTrue($parameterStrategy->validateParameterArray([1, 2, 3])); + self::assertTrue($parameterStrategy->validateParameterArray([1, 2, 'test'])); + self::assertFalse($parameterStrategy->validateParameterArray(['test', 2, 3])); $parameterStrategy->setConcatLeftover(true); // implode on [3, 4] == '3 4' - $this->assertTrue($parameterStrategy->validateParameterArray([1, 2, 3, 4])); - $this->assertTrue($parameterStrategy->validateParameterArray([1, 2, 'test', 'ing'])); + self::assertTrue($parameterStrategy->validateParameterArray([1, 2, 3, 4])); + self::assertTrue($parameterStrategy->validateParameterArray([1, 2, 'test', 'ing'])); } public function testInvalidParameterCount() @@ -146,7 +153,7 @@ public function testInvalidParameterCount() 'test' => new MockConvertibleParameter() ]); - $this->expectException(\WildPHP\Commands\Exceptions\InvalidParameterCountException::class); + $this->expectException(InvalidParameterCountException::class); $parameterStrategy->validateParameterArray([1, 2, 3]); } @@ -155,7 +162,7 @@ public function testMinMaxParameters() new ParameterStrategy(3, -1); new ParameterStrategy(0, 0); new ParameterStrategy(1, 2); - $this->expectException(\InvalidArgumentException::class); + $this->expectException(InvalidArgumentException::class); new ParameterStrategy(3, 1); } @@ -165,7 +172,7 @@ public function testInvalidParameterName() 'test' => new MockConvertibleParameter() ]); - $this->expectException(\WildPHP\Commands\Exceptions\ValidationException::class); + $this->expectException(ValidationException::class); $parameterStrategy->validateParameter('testing', 'test'); } } diff --git a/tests/ParsedCommandTest.php b/tests/ParsedCommandTest.php index de5e05d..6041c22 100644 --- a/tests/ParsedCommandTest.php +++ b/tests/ParsedCommandTest.php @@ -1,4 +1,5 @@ assertEquals('test', $parsedCommand->getCommand()); - $this->assertEquals(['test'], $parsedCommand->getArguments()); + self::assertEquals('test', $parsedCommand->getCommand()); + self::assertEquals(['test'], $parsedCommand->getArguments()); } } diff --git a/tests/PredefinedStringParameterTest.php b/tests/PredefinedStringParameterTest.php index d4a8107..ef258cd 100644 --- a/tests/PredefinedStringParameterTest.php +++ b/tests/PredefinedStringParameterTest.php @@ -1,4 +1,5 @@ assertTrue($predefinedStringParameter->validate('test')); - $this->assertFalse($predefinedStringParameter->validate('Test')); - $this->assertFalse($predefinedStringParameter->validate('ing')); + self::assertTrue($predefinedStringParameter->validate('test')); + self::assertFalse($predefinedStringParameter->validate('Test')); + self::assertFalse($predefinedStringParameter->validate('ing')); } } diff --git a/tests/ProcessedCommandTest.php b/tests/ProcessedCommandTest.php index 91ea5d7..cee4158 100644 --- a/tests/ProcessedCommandTest.php +++ b/tests/ProcessedCommandTest.php @@ -1,4 +1,5 @@ assertSame($parameterStrategy, $processedCommand->getApplicableStrategy()); - $this->assertSame(['ing'], $processedCommand->getConvertedParameters()); + self::assertSame($parameterStrategy, $processedCommand->getApplicableStrategy()); + self::assertSame(['ing'], $processedCommand->getConvertedParameters()); } } From 5ab9939b4b7df77c75380d09d1c41bb1ec821716 Mon Sep 17 00:00:00 2001 From: NanoSector Date: Tue, 14 Jul 2020 16:05:39 +0200 Subject: [PATCH 5/8] Update README --- README.md | 48 +++++++++++++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index d13d692..d27968d 100755 --- a/README.md +++ b/README.md @@ -26,52 +26,58 @@ This is the class which defines your commands. It is a storage class, meaning it hand to it. #### Creating a Command -A `Command` takes two parameters for its constructor. A callback which should be called when the command is triggered, and +A `Command` takes two parameters for its constructor. A callback which should be called when the command triggers, and one or more `ParameterStrategy` instances which will define the behavior of the command. For example: ```php -$foo = function () {}; +use WildPHP\Commands\Command; +use WildPHP\Commands\ParameterStrategy; +use WildPHP\Commands\Parameters\NumericParameter; + +$foo = static function () {}; $command = new Command($foo, new ParameterStrategy(0, 1, [ new NumericParameter(), -]); +])); ``` ### ParameterStrategy -A `ParameterStrategy` instance describes the ways in which commands may be executed. +A `ParameterStrategy` instance defines a single way in which parameters can be passed to commands. Its constructor takes a few arguments: ```php -__construct( - int $minimumParameters = -1, - int $maximumParameters = -1, - array $initialValues = [], - bool $implodeLeftover = false -) +class ParameterStrategy { + public function __construct( + int $minimumParameters = -1, + int $maximumParameters = -1, + array $initialValues = [], + bool $implodeLeftover = false + ) + {} +} ``` - `$minimumParameters` and `$maximumParameters` describe how many parameters this particular strategy may take. They can be set to -1 or false to disable either bound. However, minimum cannot be larger than maximum. - `$initialValues` is an array of `ParameterInterface` objects. More on those below. -- `$concatLeftover` is a boolean. When set to true, the strategy will concatenate all remaining parameters together into one - when the maximum parameter count is reached. For example, consider a strategy which will only take up to 2 parameters. +- `$concatLeftover` is a boolean value. When set to true, the strategy will concatenate all remaining parameters together into one + after the maximum parameter count has been reached. For example, consider a strategy which will only take up to 2 parameters. If you pass it a command with 3 parameters with this flag set, for example '!test 1 2 3', 2 and 3 will be concatenated - as `array('1', '2 3')` + as `array('1', '2 3')`. ### Available parameters #### NumericParameter Takes any value which is numeric, it uses the php `is_numeric` function internally. #### PredefinedStringParameter -Set a predefined string in its constructor and only that string will be accepted as its value, thus -any other value will be rejected. +Set a predefined string in its constructor. This parameter will only validate when this exact string matches. #### StringParameter -Returns true under any circumstance, since parameters are always strings. +Returns true under any circumstance, since parameters are always strings. Can be used as a catch-all parameter. ### CommandParser -As the name would suggest, this is the class in charge of actually parsing messages. When it does so, it hands out `ParsedCommand` -objects. As it is a utility class, its methods are called statically. +This is the class in charge of actually parsing strings. When it does so, it returns `ParsedCommand` +objects. As it is a utility class, its methods should be called statically. #### findApplicableStrategy `findApplicableStrategy(Command $commandObject, array $parameters): ParameterStrategy` @@ -87,8 +93,8 @@ Set `$prefix` for the prefix to use, which should prefix any command given. `ParsedCommand` is an object containing the command name and the original parameters given. ### CommandProcessor -This class processes a ParsedCommand further and provides the value conversion facilities. -Moreover, when instantiated, it acts as a storage facility for commands so they need not be stored elsewhere. +This class processes a ParsedCommand and provides the value conversion facilities. +Moreover, when instantiated, it acts as a storage facility for command objects. #### processParsedCommand The heart of this class is this static function. @@ -96,7 +102,7 @@ The heart of this class is this static function. `public static function processParsedCommand(ParsedCommand $parsedCommand, Command $command): ProcessedCommand` It takes a `ParsedCommand` object and accompanying `Command` object and processes it into a `ProcessedCommand` object, -which contains the original `ParsedCommand` data plus the callback, the converted parameter values and also the applied strategy used for conversion. +which contains the original `ParsedCommand` data plus the callback, the converted parameter values, and the applied strategy used for conversion. ### process `process(ParsedCommand $parsedCommand): ProcessedCommand` From 3bdb5b88f0ddf38b8c8eeecbd6d1bb6de4356c3a Mon Sep 17 00:00:00 2001 From: NanoSector Date: Tue, 14 Jul 2020 16:05:53 +0200 Subject: [PATCH 6/8] Run inspections --- src/CommandParser.php | 10 +++++----- src/CommandProcessor.php | 2 +- src/ParameterStrategy.php | 2 +- src/Parameters/PredefinedStringParameter.php | 4 ++-- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/CommandParser.php b/src/CommandParser.php index 8d96e6b..81b3207 100644 --- a/src/CommandParser.php +++ b/src/CommandParser.php @@ -36,7 +36,7 @@ public static function findApplicableStrategy(Command $commandObject, array $par } } - throw new NoApplicableStrategiesException(); + throw new NoApplicableStrategiesException('No applicable strategies were found'); } /** @@ -50,12 +50,12 @@ public static function parseFromString(string $string, string $prefix = '!'): Pa $messageParts = explode(' ', trim($string)); $firstPart = array_shift($messageParts); - if (strlen($firstPart) == strlen($prefix)) { - throw new ParseException(); + if (strlen($firstPart) === strlen($prefix)) { + throw new ParseException('This command consists of only a command prefix'); } - if (substr($firstPart, 0, strlen($prefix)) != $prefix) { - throw new ParseException(); + if (strpos($firstPart, $prefix) !== 0) { + throw new ParseException('The command prefix is not at the beginning of the given command'); } $command = substr($firstPart, strlen($prefix)); diff --git a/src/CommandProcessor.php b/src/CommandProcessor.php index 5a76ed4..8919a62 100644 --- a/src/CommandProcessor.php +++ b/src/CommandProcessor.php @@ -71,7 +71,7 @@ public function findCommand(string $command): Command $dictionary = $this->getCommandCollection(); if (!$dictionary->offsetExists($command)) { - throw new Exceptions\CommandNotFoundException(); + throw new CommandNotFoundException('The given command was not (yet) registered in the CommandProcessor'); } /** @var Command $commandObject */ diff --git a/src/ParameterStrategy.php b/src/ParameterStrategy.php index 3f593b4..e633209 100644 --- a/src/ParameterStrategy.php +++ b/src/ParameterStrategy.php @@ -90,7 +90,7 @@ public function validateParameterArray(array $args): bool $names = array_keys((array)$this); if (!$this->validateParameterCount($args)) { - throw new InvalidParameterCountException(); + throw new InvalidParameterCountException('The command has an unsatisfactory amount of parameters'); } if ($this->shouldConcatLeftover()) { diff --git a/src/Parameters/PredefinedStringParameter.php b/src/Parameters/PredefinedStringParameter.php index a857c85..9a61de4 100644 --- a/src/Parameters/PredefinedStringParameter.php +++ b/src/Parameters/PredefinedStringParameter.php @@ -18,8 +18,8 @@ class PredefinedStringParameter extends Parameter */ public function __construct(string $expected) { - parent::__construct(function (string $value) use ($expected) { - return $value == $expected; + parent::__construct(static function (string $value) use ($expected) { + return $value === $expected; }); } } From 20634ae3a2c44c7df9d675931b1f5f50ea10fbc2 Mon Sep 17 00:00:00 2001 From: NanoSector Date: Tue, 14 Jul 2020 16:06:03 +0200 Subject: [PATCH 7/8] Bump code coverage back to 100% --- tests/ProcessedCommandTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/ProcessedCommandTest.php b/tests/ProcessedCommandTest.php index cee4158..b2d3f4c 100644 --- a/tests/ProcessedCommandTest.php +++ b/tests/ProcessedCommandTest.php @@ -26,5 +26,6 @@ public function testGetApplicableStrategy() self::assertSame($parameterStrategy, $processedCommand->getApplicableStrategy()); self::assertSame(['ing'], $processedCommand->getConvertedParameters()); + self::assertSame([$this, 'foo'], $processedCommand->getCallback()); } } From 1477d33a8dfb18bdd4fde08fb578d1ccf3c95e4b Mon Sep 17 00:00:00 2001 From: NanoSector Date: Tue, 14 Jul 2020 16:08:44 +0200 Subject: [PATCH 8/8] Add PHP 7.4 to scrutinizer test suite --- .scrutinizer.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.scrutinizer.yml b/.scrutinizer.yml index afd1920..db53271 100644 --- a/.scrutinizer.yml +++ b/.scrutinizer.yml @@ -15,6 +15,10 @@ build: environment: php: 7.3 + tests-php74: + environment: + php: 7.4 + analysis: tests: override: