From 194de711ab8a4d11e263d031509df2e161237073 Mon Sep 17 00:00:00 2001 From: Shosho Date: Mon, 13 Jun 2022 11:54:06 +0200 Subject: [PATCH] Feat/support php8 (#68) * feat: php8 support * feat: GHA ? * feat: removing composer.lock * feat: removing composer.lock * feat: type-hints in every files o/ * feat: self-review * feat: self review still * feat: fixing a dumb error :) * feat: fixing another error * feat: review * feat: review * feat: review * feat: constructor property promotion * feat: type hint ! * feat(quality): added PHPStan and rector * feat(quality): apply rector rules * feat(quality): fix phpstan warnings * feat(quality): removed useless constructor * feat(quality): updated ignore list * feat(quality): updated types and misc phpstan adjustments * feat(quality): apply rector & php-cs-fixer * feat(quality): simplify some code after adding better types * feat: merged vclavreul PR Co-authored-by: vderudet Co-authored-by: Valentin Clavreul --- .coke | 11 - .github/workflows/ci.yml | 4 +- .gitignore | 5 +- .php-cs-fixer.php | 30 +- composer.json | 14 +- phpstan.neon | 70 ++ rector.php | 21 + .../LogBridgeBundle/Config/Configuration.php | 46 +- .../Bundle/LogBridgeBundle/Config/Filter.php | 115 +-- .../Config/FilterCollection.php | 116 +-- .../LogBridgeBundle/Config/FilterParser.php | 179 +--- .../LogBridgeBundle/Config/ParseException.php | 2 + .../Bundle/LogBridgeBundle/Config/Parser.php | 56 +- .../CompilerPass/LoggerValidationPass.php | 6 +- .../MatcherStatusTypeCompilerPass.php | 4 +- .../DependencyInjection/Configuration.php | 2 + .../M6WebLogBridgeExtension.php | 18 +- .../EventDispatcher/BuilderEvent.php | 52 +- .../EventDispatcher/LogExceptionListener.php | 17 +- .../EventDispatcher/LogRequestListener.php | 55 +- .../Formatter/DefaultFormatter.php | 118 +-- .../Formatter/ExceptionFormatter.php | 38 +- .../Formatter/ExceptionFormatterInterface.php | 7 +- .../Formatter/FormatterInterface.php | 18 +- .../Bundle/LogBridgeBundle/Logger/Logger.php | 10 +- .../LogBridgeBundle/M6WebLogBridgeBundle.php | 7 +- .../LogBridgeBundle/Matcher/Builder.php | 148 +--- .../Matcher/BuilderInterface.php | 25 +- .../Matcher/Dumper/PhpMatcherDumper.php | 221 ++--- .../Matcher/MatcherInterface.php | 84 +- .../LogBridgeBundle/Matcher/MatcherProxy.php | 110 +-- .../Matcher/Status/Type/AbstractType.php | 52 +- .../Matcher/Status/Type/CompleteType.php | 29 +- .../Matcher/Status/Type/RangeType.php | 17 +- .../Matcher/Status/Type/SimpleType.php | 17 +- .../Matcher/Status/Type/TypeInterface.php | 19 +- .../Matcher/Status/Type/UpperType.php | 19 +- .../Matcher/Status/TypeManager.php | 36 +- .../Resources/config/services.yml | 1 - .../Matcher/Status/Type/Sample1Type.php | 16 +- .../Matcher/Status/Type/Sample2Type.php | 16 +- .../Matcher/Status/Type/Sample3Type.php | 16 +- .../Resources/cache/TestLogMatcher.php | 789 ------------------ .../LogBridgeBundle/Tests/Units/BaseTest.php | 14 +- .../Tests/Units/Config/Configuration.php | 22 +- .../Tests/Units/Config/Filter.php | 8 +- .../Tests/Units/Config/FilterCollection.php | 16 +- .../Tests/Units/Config/FilterParser.php | 22 +- .../Tests/Units/Config/Parser.php | 24 +- .../Units/EventDispatcher/BuilderEvent.php | 2 +- .../EventDispatcher/LogExceptionListener.php | 2 +- .../Units/Formatter/DefaultFormatter.php | 25 +- .../Units/Formatter/ExceptionFormatter.php | 43 +- .../Tests/Units/Matcher/BaseMatcher.php | 12 +- .../Tests/Units/Matcher/Builder.php | 23 +- .../Units/Matcher/Dumper/PhpMatcherDumper.php | 2 +- .../Tests/Units/Matcher/MatcherProxy.php | 20 +- .../Matcher/Status/Type/AbstractType.php | 36 +- .../Matcher/Status/Type/CompleteType.php | 7 +- .../Units/Matcher/Status/Type/RangeType.php | 7 +- .../Units/Matcher/Status/Type/SimpleType.php | 7 +- .../Units/Matcher/Status/Type/UpperType.php | 7 +- .../Units/Matcher/Status/TypeManager.php | 54 +- 63 files changed, 705 insertions(+), 2284 deletions(-) delete mode 100644 .coke create mode 100644 phpstan.neon create mode 100644 rector.php delete mode 100644 src/M6Web/Bundle/LogBridgeBundle/Tests/Fixtures/Resources/cache/TestLogMatcher.php diff --git a/.coke b/.coke deleted file mode 100644 index 61b061a..0000000 --- a/.coke +++ /dev/null @@ -1,11 +0,0 @@ -# Configuration file for Coke, "Enjoy sniffing your code" : https://github.com/M6Web/Coke - -# Standard used by PHP CodeSniffer (required) -standard=vendor/m6web/symfony2-coding-standard/Symfony2 - -# White list of files and directories (optional) -src/ - -# Black list of files and directories (optional) -!Tests -!vendor/ diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2db2211..41d8be7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -9,7 +9,7 @@ jobs: - uses: actions/checkout@master - uses: shivammathur/setup-php@v2 with: - php-version: 7.4 + php-version: 8.0 - run: composer install --prefer-dist --no-interaction - run: bin/php-cs-fixer fix --dry-run --stop-on-violation --diff --ansi @@ -18,7 +18,7 @@ jobs: runs-on: ubuntu-18.04 strategy: matrix: - version: ['7.1', '7.2', '7.3', '7.4', '8.0', '8.1'] + version: ['8.0', '8.1'] flags: ['', '--prefer-lowest'] fail-fast: false steps: diff --git a/.gitignore b/.gitignore index ded9c09..515f55b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ bin/ vendor/ -src/M6Web/Bundle/LogBridgeBundle/Tests/Resources/cache/* -composer.lock +src/M6Web/Bundle/LogBridgeBundle/Tests/Fixtures/Resources/cache/* .php_cs.cache +.php-cs-fixer.cache +composer.lock diff --git a/.php-cs-fixer.php b/.php-cs-fixer.php index a618646..421ed1d 100644 --- a/.php-cs-fixer.php +++ b/.php-cs-fixer.php @@ -1,22 +1,12 @@ getRules() + ['increment_style' => false]; - } -}; - -$config->getFinder() - ->in([ - __DIR__.'/src' - ])->exclude([ - 'Tests' - ]); - -return $config; +return (new \M6Web\CS\Config\BedrockStreaming()) + ->setFinder( + PhpCsFixer\Finder::create() + ->in([ + __DIR__.'/src' + ]) + ->exclude([ + 'Tests' + ]) + ); diff --git a/composer.json b/composer.json index b15963d..7fced9b 100644 --- a/composer.json +++ b/composer.json @@ -8,19 +8,21 @@ "minimum-stability" : "dev", "prefer-stable": true, "require": { - "php": ">=7.1.3", + "php": ">=8.0", "symfony/config": "^4.4|^5.0|^6.0", "symfony/yaml": "^4.4|^5.0|^6.0", "symfony/routing": "^4.4|^5.0|^6.0", "symfony/expression-language": "^4.4|^5.0|^6.0", - "symfony/security-core": "^4.4|^5.0|^6.0" + "symfony/security-core": "^4.4|^5.0|^6.0", + "symfony/dependency-injection": "^4.4|^5.0|^6.0", + "symfony/http-kernel": "^4.4|^5.0|^6.0" }, "require-dev": { "atoum/atoum": "^3.4|^4.0", - "friendsofphp/php-cs-fixer": "^2.19", - "m6web/symfony2-coding-standard": "~1.2", - "m6web/php-cs-fixer-config": "^1.0", - "symfony/http-foundation": "^4.4|^5.0|^6.0" + "m6web/php-cs-fixer-config": "^2.1", + "symfony/http-foundation": "^4.4|^5.0|^6.0", + "phpstan/phpstan": "^1.7", + "rector/rector": "^0.13.2" }, "extra": { "branch-alias": { diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..49d0dcb --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,70 @@ +parameters: + level: max + paths: + - src + excludePaths: + # This class is deprecated, we don't care about warnings + - src/M6Web/Bundle/LogBridgeBundle/EventDispatcher/BuilderEvent.php + - src/M6Web/Bundle/LogBridgeBundle/Tests/* + + # Errors from DependencyInjection : hard to fix, not that useful + ignoreErrors: + - + message: "#^Parameter \\#1 \\$id of method Symfony\\\\Component\\\\DependencyInjection\\\\ContainerBuilder\\:\\:findDefinition\\(\\) expects string, array\\|bool\\|float\\|int\\|string\\|UnitEnum\\|null given\\.$#" + count: 1 + path: src/M6Web/Bundle/LogBridgeBundle/DependencyInjection/CompilerPass/LoggerValidationPass.php + + - + message: "#^Parameter \\#1 \\$object_or_class of function class_implements expects object\\|string, array\\|bool\\|float\\|int\\|string\\|UnitEnum\\|null given\\.$#" + count: 1 + path: src/M6Web/Bundle/LogBridgeBundle/DependencyInjection/CompilerPass/LoggerValidationPass.php + + - + message: "#^Parameter \\#1 \\$string of function substr expects string, string\\|null given\\.$#" + count: 1 + path: src/M6Web/Bundle/LogBridgeBundle/DependencyInjection/CompilerPass/LoggerValidationPass.php + + - + message: "#^Parameter \\#2 \\$haystack of function in_array expects array, array\\\\|false given\\.$#" + count: 1 + path: src/M6Web/Bundle/LogBridgeBundle/DependencyInjection/CompilerPass/LoggerValidationPass.php + + - + message: "#^Parameter \\#2 \\$subject of function preg_match expects string, string\\|null given\\.$#" + count: 1 + path: src/M6Web/Bundle/LogBridgeBundle/DependencyInjection/CompilerPass/LoggerValidationPass.php + + - + message: "#^Parameter \\#2 \\.\\.\\.\\$values of function sprintf expects bool\\|float\\|int\\|string\\|null, array\\|bool\\|float\\|int\\|string\\|UnitEnum\\|null given\\.$#" + count: 1 + path: src/M6Web/Bundle/LogBridgeBundle/DependencyInjection/CompilerPass/LoggerValidationPass.php + + - + message: "#^Call to an undefined method Symfony\\\\Component\\\\Config\\\\Definition\\\\Builder\\\\NodeDefinition\\:\\:children\\(\\)\\.$#" + count: 1 + path: src/M6Web/Bundle/LogBridgeBundle/DependencyInjection/Configuration.php + + - + message: "#^Method M6Web\\\\Bundle\\\\LogBridgeBundle\\\\DependencyInjection\\\\M6WebLogBridgeExtension\\:\\:load\\(\\) has parameter \\$configs with no value type specified in iterable type array\\.$#" + count: 1 + path: src/M6Web/Bundle/LogBridgeBundle/DependencyInjection/M6WebLogBridgeExtension.php + + - + message: "#^Parameter \\#1 \\$class of class Symfony\\\\Component\\\\DependencyInjection\\\\Definition constructor expects string\\|null, array\\|bool\\|float\\|int\\|string\\|UnitEnum\\|null given\\.$#" + count: 2 + path: src/M6Web/Bundle/LogBridgeBundle/DependencyInjection/M6WebLogBridgeExtension.php + + - + message: "#^Parameter \\#1 \\$id of class Symfony\\\\Component\\\\DependencyInjection\\\\Reference constructor expects string, array\\|bool\\|float\\|int\\|string\\|UnitEnum\\|null given\\.$#" + count: 3 + path: src/M6Web/Bundle/LogBridgeBundle/DependencyInjection/M6WebLogBridgeExtension.php + + - + message: "#^Parameter \\#1 \\$id of method Symfony\\\\Component\\\\DependencyInjection\\\\ContainerBuilder\\:\\:setDefinition\\(\\) expects string, array\\|bool\\|float\\|int\\|string\\|UnitEnum\\|null given\\.$#" + count: 2 + path: src/M6Web/Bundle/LogBridgeBundle/DependencyInjection/M6WebLogBridgeExtension.php + + - + message: "#^Parameter \\#1 \\.\\.\\.\\$arrays of function array_merge expects array, array\\|bool\\|float\\|int\\|string\\|UnitEnum\\|null given\\.$#" + count: 1 + path: src/M6Web/Bundle/LogBridgeBundle/DependencyInjection/M6WebLogBridgeExtension.php diff --git a/rector.php b/rector.php new file mode 100644 index 0000000..851cef5 --- /dev/null +++ b/rector.php @@ -0,0 +1,21 @@ +paths([ + __DIR__ . '/src' + ]); + + // register a single rule + $rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class); + + // define sets of rules + $rectorConfig->sets([ + LevelSetList::UP_TO_PHP_80 + ]); +}; diff --git a/src/M6Web/Bundle/LogBridgeBundle/Config/Configuration.php b/src/M6Web/Bundle/LogBridgeBundle/Config/Configuration.php index 8e3c571..a666208 100644 --- a/src/M6Web/Bundle/LogBridgeBundle/Config/Configuration.php +++ b/src/M6Web/Bundle/LogBridgeBundle/Config/Configuration.php @@ -1,5 +1,7 @@ filters = null; - $this->activeFilters = null; - } + /** @var string[] */ + private ?array $activeFilters = null; /** - * setActiveFilters - * - * @return Configuration + * @param string[] $activeFilters */ - public function setActiveFilters(array $activeFilters) + public function setActiveFilters(array $activeFilters): self { $this->activeFilters = $activeFilters; @@ -35,35 +25,21 @@ public function setActiveFilters(array $activeFilters) } /** - * getActiveFilters - * - * @return array + * @return string[]|null */ - public function getActiveFilters() + public function getActiveFilters(): ?array { return $this->activeFilters; } - /** - * setFilters - * - * @param FilterCollection $filters Filters - * - * @return Configuration - */ - public function setFilters(FilterCollection $filters) + public function setFilters(FilterCollection $filters): self { $this->filters = $filters; return $this; } - /** - * getFilters - * - * @return FilterCollection - */ - public function getFilters() + public function getFilters(): ?FilterCollection { return $this->filters; } diff --git a/src/M6Web/Bundle/LogBridgeBundle/Config/Filter.php b/src/M6Web/Bundle/LogBridgeBundle/Config/Filter.php index b532ca7..e2ee4ad 100644 --- a/src/M6Web/Bundle/LogBridgeBundle/Config/Filter.php +++ b/src/M6Web/Bundle/LogBridgeBundle/Config/Filter.php @@ -1,5 +1,7 @@ name = $name; - $this->route = null; - $this->method = null; - $this->status = null; - $this->level = null; - $this->options = []; } - /** - * getName - * - * @return string - */ - public function getName() + public function getName(): string { return $this->name; } - /** - * setRoute - * - * @param string $route Route - * - * @return Filter - */ - public function setRoute($route) + public function setRoute(?string $route): self { $this->route = $route; return $this; } - /** - * getRoute - * - * @return string - */ - public function getRoute() + public function getRoute(): ?string { return $this->route; } /** - * setMethod - * - * @param mixed $method Method - * - * @return Filter + * @param string[]|null $method */ - public function setMethod($method) + public function setMethod(?array $method): self { $this->method = $method; @@ -89,23 +54,17 @@ public function setMethod($method) } /** - * getMethod - * - * @return mixed + * @return string[]|null */ - public function getMethod() + public function getMethod(): ?array { return $this->method; } /** - * setStatus - * - * @param mixed $status Http code status - * - * @return Filter + * @param int[]|null $status */ - public function setStatus($status) + public function setStatus(?array $status): self { $this->status = $status; @@ -113,45 +72,29 @@ public function setStatus($status) } /** - * getStatus - * - * @return mixed + * @return int[]|null */ - public function getStatus() + public function getStatus(): ?array { return $this->status; } - /** - * setLevel - * - * @param mixed $level Log level - * - * @return Filter - */ - public function setLevel($level) + public function setLevel(?string $level): self { $this->level = $level; return $this; } - /** - * getLevel - * - * @return mixed - */ - public function getLevel() + public function getLevel(): ?string { return $this->level; } /** - * set filter options - * - * @return Filter + * @param array{post_parameters?: bool, response_body?: bool} $options */ - public function setOptions(array $options) + public function setOptions(array $options): self { $this->options = $options; @@ -159,11 +102,9 @@ public function setOptions(array $options) } /** - * get filter options - * - * @return array + * @return array{post_parameters?: bool, response_body?: bool} */ - public function getOptions() + public function getOptions(): array { return $this->options; } diff --git a/src/M6Web/Bundle/LogBridgeBundle/Config/FilterCollection.php b/src/M6Web/Bundle/LogBridgeBundle/Config/FilterCollection.php index 80a211c..228d5dc 100644 --- a/src/M6Web/Bundle/LogBridgeBundle/Config/FilterCollection.php +++ b/src/M6Web/Bundle/LogBridgeBundle/Config/FilterCollection.php @@ -1,47 +1,35 @@ */ class FilterCollection implements \Iterator { - /** @var int */ - protected $iterator; + protected int $iterator = 0; - /** @var array */ - protected $keys; + /** @var string[] */ + protected array $keys = []; - /** @var array */ - protected $values; + /** @var array */ + protected array $values = []; /** - * __construct - * - * @internal param array $filters Filters + * @param Filter[] $items */ public function __construct(array $items = []) { - $this->iterator = 0; - $this->keys = []; - $this->values = []; - foreach ($items as $item) { $this->add($item); } } - /** - * add - * - * @internal param \M6Web\Bundle\LogBridgeBundle\Config\Filter $filter Filter - * - * @return FilterCollection - */ - public function add(Filter $item) + public function add(Filter $item): FilterCollection { - if (!in_array($item->getName(), $this->keys)) { + if (!in_array($item->getName(), $this->keys, true)) { $this->keys[] = $item->getName(); } @@ -50,34 +38,15 @@ public function add(Filter $item) return $this; } - /** - * getByName - * - * @param string $name - * - * @return Filter - */ - public function getByName($name) + public function getByName(string $name): ?Filter { - if (!isset($this->values[$name])) { - return null; - } - - return $this->values[$name]; + return $this->values[$name] ?? null; } - /** - * remove - * - * @param Filter $filter Filter - * - * @return bool - */ - public function remove(Filter $filter) + public function remove(Filter $filter): bool { - if ($key = array_search($filter->getName(), $this->keys)) { - unset($this->keys[$key]); - unset($this->values[$filter->getName()]); + if ($key = array_search($filter->getName(), $this->keys, true)) { + unset($this->keys[$key], $this->values[$filter->getName()]); return true; } @@ -85,15 +54,7 @@ public function remove(Filter $filter) return false; } - /** - * get - * Get item - * - * @param int $iterator - * - * @return mixed - */ - public function get($iterator) + public function get(int $iterator): ?Filter { if ($key = $this->getKey($iterator)) { return $this->values[$key]; @@ -103,44 +64,27 @@ public function get($iterator) } /** - * getName * Define filter name with iterator position - * - * @param string $iterator - * - * @return string */ - public function getKey($iterator) + public function getKey(int $iterator): string { - return isset($this->keys[$iterator]) ? $this->keys[$iterator] : ''; + return $this->keys[$iterator] ?? ''; } /** - * getkeys - * - * @return array + * @return string[] */ - public function getKeys() + public function getKeys(): array { return $this->keys; } - /** - * current - * - * @return Filter - */ - public function current() + public function current(): ?Filter { return $this->get($this->iterator); } - /** - * key - * - * @return int - */ - public function key() + public function key(): int { return $this->iterator; } @@ -155,22 +99,12 @@ public function rewind(): void $this->iterator = 0; } - /** - * valid - * - * @return bool - */ - public function valid() + public function valid(): bool { return isset($this->keys[$this->iterator]); } - /** - * count - * - * @return int - */ - public function count() + public function count(): int { return count($this->values); } diff --git a/src/M6Web/Bundle/LogBridgeBundle/Config/FilterParser.php b/src/M6Web/Bundle/LogBridgeBundle/Config/FilterParser.php index f1d8f0d..dcf9d57 100644 --- a/src/M6Web/Bundle/LogBridgeBundle/Config/FilterParser.php +++ b/src/M6Web/Bundle/LogBridgeBundle/Config/FilterParser.php @@ -1,5 +1,7 @@ |null */ + protected ?string $filterClass = null; - /** - * __construct - * - * @param RouterInterface $router Router service - */ - public function __construct(RouterInterface $router = null) + public function __construct(protected ?RouterInterface $router = null) { - $this->router = $router; - $this->filterClass = ''; } - /** - * createFilter - * - * @param string $name Filter name - * - * @return Filter - */ - protected function createFilter($name) + protected function createFilter(string $name): Filter { - if ($this->filterClass) { - return (new \ReflectionClass($this->filterClass))->newInstanceArgs(['name' => $name]); + if ($this->filterClass !== null) { + return (new \ReflectionClass($this->filterClass))->newInstanceArgs([$name]); } return new Filter($name); } - /** - * isRoute - * - * @param string $name Route name - * - * @return bool - */ - protected function isRoute($name) + protected function isRoute(string $name): bool { - return $this->router->getRouteCollection()->get($name) ? true : false; + return $this->router?->getRouteCollection()->get($name) !== null; } /** - * parse - * - * @param string $name name - * @param array $config configuration - * - * @throws ParseException - * - * @internal param array $filterConfig - * - * @return Filter + * @param array{ + * route?: string, + * method?: string[], + * status?: int[], + * level?: string, + * options?: array{post_parameters?: bool, response_body?: bool} + * } $config */ - public function parse($name, array $config) + public function parse(string $name, array $config): Filter { - $filter = $this->createFilter($name); - - if (!array_key_exists('route', $config) || !array_key_exists('method', $config) || !array_key_exists('status', $config)) { + if ( + !array_key_exists('route', $config) || + !array_key_exists('method', $config) || + !array_key_exists('status', $config) + ) { throw new ParseException(sprintf('Undefined "route", "method" or "status" parameter from filter "%s"', $name)); } @@ -93,96 +70,35 @@ public function parse($name, array $config) $config['level'] = self::DEFAULT_LEVEL; } + $filter = $this->createFilter($name) + ->setMethod($config['method']) + ->setStatus($config['status']); + $this->parseRoute($filter, $config['route']); - $this->parseMethod($filter, $config['method']); - $this->parseStatus($filter, $config['status']); $this->parseLevel($filter, $config['level']); - $filter->setOptions(isset($config['options']) ? $config['options'] : []); - - return $filter; + return $filter->setOptions($config['options'] ?? []); } - /** - * parseRoute - * - * @param Filter $filter Filter - * @param mixed $route Route parameter value - * - * @throws ParseException - */ - protected function parseRoute(Filter $filter, $route) + protected function parseRoute(Filter $filter, ?string $route): void { - if (!is_null($route) && !$this->isRoute($route)) { + if ($route !== null && !$this->isRoute($route)) { throw new ParseException(sprintf('Undefined route "%s" from router service', $route)); } $filter->setRoute($route); } - /** - * parseMethod - * - * @param Filter $filter Filter - * @param mixed $method Method parameter value - * - * @throws ParseException - */ - protected function parseMethod(Filter $filter, $method) - { - if (!is_array($method) && !is_null($method)) { - throw new ParseException(sprintf('Unrecognized value "%s" from method parameter', $method)); - } - - $filter->setMethod($method); - } - - /** - * parseStatus - * - * @param Filter $filter Filter - * @param mixed $status Status parameter value - * - * @throws ParseException - */ - protected function parseStatus(Filter $filter, $status) + protected function parseLevel(Filter $filter, ?string $level): void { - if (!is_array($status) && !is_null($status)) { - throw new ParseException(sprintf('Unrecognized value "%s" from status parameter', $status)); - } - - $filter->setStatus($status); - } - - /** - * parseLevel - * - * @param Filter $filter Filter - * @param mixed $level Level parameter value - * - * @throws ParseException - */ - protected function parseLevel(Filter $filter, $level) - { - if (!is_string($level) && !is_null($level)) { - throw new ParseException(sprintf('Unrecognized value "%s" from level parameter', $level)); - } - - if (!in_array($level, $this->allowedLevels)) { + if (!in_array($level, $this->allowedLevels, true)) { throw new ParseException(sprintf('Invalid value "%s" from level parameter, allowed %s', $level, implode(', ', $this->allowedLevels))); } $filter->setLevel($level); } - /** - * setRouter - * - * @param RouterInterface $router Router - * - * @return FilterParser - */ - public function setRouter(RouterInterface $router) + public function setRouter(RouterInterface $router): self { $this->router = $router; @@ -190,23 +106,17 @@ public function setRouter(RouterInterface $router) } /** - * setFilterClass - * - * @param string $filterClass Filter class name - * - * @return FilterParser - * - * @throws \RuntimeException + * @param class-string $filterClass */ - public function setFilterClass($filterClass) + public function setFilterClass(string $filterClass): self { $reflection = new \ReflectionClass($filterClass); if ( - !$reflection->isInstantiable() - || !$reflection->isSubclassOf('M6Web\Bundle\LogBridgeBundle\Config\Filter') + !$reflection->isInstantiable() || + !$reflection->isSubclassOf(Filter::class) ) { - throw new \RuntimeException(sprintf('"%s" is not instantiable or is not a subclass of "M6Web\Bundle\LogBridgeBundle\Config\Filter"', $filterClass)); + throw new \RuntimeException(sprintf('"%s" is not instantiable or is not a subclass of "%s"', $filterClass, Filter::class)); } $this->filterClass = $filterClass; @@ -214,12 +124,7 @@ public function setFilterClass($filterClass) return $this; } - /** - * getFilterClass - * - * @return string - */ - public function getFilterClass() + public function getFilterClass(): ?string { return $this->filterClass; } diff --git a/src/M6Web/Bundle/LogBridgeBundle/Config/ParseException.php b/src/M6Web/Bundle/LogBridgeBundle/Config/ParseException.php index cf17083..40a6829 100644 --- a/src/M6Web/Bundle/LogBridgeBundle/Config/ParseException.php +++ b/src/M6Web/Bundle/LogBridgeBundle/Config/ParseException.php @@ -1,5 +1,7 @@ router = $router; - $this->filterParser = null; } /** - * createFilterCollection - * - * @param array $filters Filters configuration - * - * @return FilterCollection + * @param array $filters */ - protected function createFilterCollection(array $filters) + protected function createFilterCollection(array $filters): FilterCollection { $collection = new FilterCollection(); @@ -45,14 +38,20 @@ protected function createFilterCollection(array $filters) } /** - * parse * Load Log Request filter configuration * - * @internal param array $config Config - * - * @return Configuration + * @param array{ + * filters?: array , + * active_filters?: string[] + * } $params */ - public function parse(array $params) + public function parse(array $params): Configuration { $config = new Configuration(); $filters = new FilterCollection(); @@ -70,14 +69,9 @@ public function parse(array $params) return $config; } - /** - * getFilterParser - * - * @return FilterParser - */ - public function getFilterParser() + public function getFilterParser(): FilterParser { - if (!$this->filterParser) { + if ($this->filterParser === null) { $this->filterParser = new FilterParser($this->router); } diff --git a/src/M6Web/Bundle/LogBridgeBundle/DependencyInjection/CompilerPass/LoggerValidationPass.php b/src/M6Web/Bundle/LogBridgeBundle/DependencyInjection/CompilerPass/LoggerValidationPass.php index 26b4718..a016651 100644 --- a/src/M6Web/Bundle/LogBridgeBundle/DependencyInjection/CompilerPass/LoggerValidationPass.php +++ b/src/M6Web/Bundle/LogBridgeBundle/DependencyInjection/CompilerPass/LoggerValidationPass.php @@ -1,5 +1,7 @@ hasParameter('m6web_log_bridge.logger.service')) { return; @@ -28,7 +30,7 @@ public function process(ContainerBuilder $container) $loggerClass = $container->getParameter($loggerClassParameter); } - if (!in_array('Psr\Log\LoggerInterface', class_implements($loggerClass))) { + if (!in_array(\Psr\Log\LoggerInterface::class, class_implements($loggerClass), true)) { throw new \InvalidArgumentException(sprintf('Class "%s" must be implement "Psr\Log\LoggerInterface"', $loggerClass)); } } diff --git a/src/M6Web/Bundle/LogBridgeBundle/DependencyInjection/CompilerPass/MatcherStatusTypeCompilerPass.php b/src/M6Web/Bundle/LogBridgeBundle/DependencyInjection/CompilerPass/MatcherStatusTypeCompilerPass.php index 3916f3c..d581939 100644 --- a/src/M6Web/Bundle/LogBridgeBundle/DependencyInjection/CompilerPass/MatcherStatusTypeCompilerPass.php +++ b/src/M6Web/Bundle/LogBridgeBundle/DependencyInjection/CompilerPass/MatcherStatusTypeCompilerPass.php @@ -1,5 +1,7 @@ has('m6web_log_bridge.matcher.status.type_manager')) { return; diff --git a/src/M6Web/Bundle/LogBridgeBundle/DependencyInjection/Configuration.php b/src/M6Web/Bundle/LogBridgeBundle/DependencyInjection/Configuration.php index a5b23b1..fa03990 100644 --- a/src/M6Web/Bundle/LogBridgeBundle/DependencyInjection/Configuration.php +++ b/src/M6Web/Bundle/LogBridgeBundle/DependencyInjection/Configuration.php @@ -1,5 +1,7 @@ processConfiguration($configuration, $configs); @@ -54,12 +56,7 @@ public function load(array $configs, ContainerBuilder $container) $this->loadRequestListener($container); } - /** - * loadRequestListener - * - * @param ContainerBuilder $container Container - */ - protected function loadRequestListener(ContainerBuilder $container) + protected function loadRequestListener(ContainerBuilder $container): void { $className = $container->getParameter('m6web_log_bridge.log_request_listener.class'); $serviceName = $container->getParameter('m6web_log_bridge.log_request_listener.name'); @@ -84,12 +81,7 @@ protected function loadRequestListener(ContainerBuilder $container) $container->setDefinition($serviceName, $definition); } - /** - * loadExceptionListener - * - * @param ContainerBuilder $container Container - */ - protected function loadExceptionListener(ContainerBuilder $container) + protected function loadExceptionListener(ContainerBuilder $container): void { $className = $container->getParameter('m6web_log_bridge.log_exception_listener.class'); $serviceName = $container->getParameter('m6web_log_bridge.log_exception_listener.name'); diff --git a/src/M6Web/Bundle/LogBridgeBundle/EventDispatcher/BuilderEvent.php b/src/M6Web/Bundle/LogBridgeBundle/EventDispatcher/BuilderEvent.php index 2c980e7..a968f7a 100644 --- a/src/M6Web/Bundle/LogBridgeBundle/EventDispatcher/BuilderEvent.php +++ b/src/M6Web/Bundle/LogBridgeBundle/EventDispatcher/BuilderEvent.php @@ -1,5 +1,7 @@ builder = $builder; - $this->config = $config; } - /** - * setBuilder - * - * @param BuilderInterface $builder Builder - * - * @return BuilderEvent - */ - public function setBuilder(BuilderInterface $builder) + public function setBuilder(BuilderInterface $builder): self { $this->builder = $builder; return $this; } - /** - * getBuilder - * - * @return BuilderInterface - */ - public function getBuilder() + public function getBuilder(): BuilderInterface { return $this->builder; } - /** - * setConfig - * - * @param array $config Config - * - * @return BuilderEvent - */ - public function setConfig(array $config) + public function setConfig(array $config): self { $this->config = $config; return $this; } - /** - * getConfig - * - * @return array - */ - public function getConfig() + public function getConfig(): array { return $this->config; } diff --git a/src/M6Web/Bundle/LogBridgeBundle/EventDispatcher/LogExceptionListener.php b/src/M6Web/Bundle/LogBridgeBundle/EventDispatcher/LogExceptionListener.php index 45b53c6..62ca1ba 100644 --- a/src/M6Web/Bundle/LogBridgeBundle/EventDispatcher/LogExceptionListener.php +++ b/src/M6Web/Bundle/LogBridgeBundle/EventDispatcher/LogExceptionListener.php @@ -1,5 +1,7 @@ requestExceptionAttribute = $requestExceptionAttribute; } /** * React to an exception to give error message to log bridge */ - public function onKernelException(ExceptionEvent $event) + public function onKernelException(ExceptionEvent $event): void { - $exception = $event->getThrowable(); - $request = $event->getRequest(); $request->setRequestFormat('json'); - $request->attributes->add([$this->requestExceptionAttribute => $exception]); + $request->attributes->add([$this->requestExceptionAttribute => $event->getThrowable()]); } } diff --git a/src/M6Web/Bundle/LogBridgeBundle/EventDispatcher/LogRequestListener.php b/src/M6Web/Bundle/LogBridgeBundle/EventDispatcher/LogRequestListener.php index 0fd19af..a347beb 100644 --- a/src/M6Web/Bundle/LogBridgeBundle/EventDispatcher/LogRequestListener.php +++ b/src/M6Web/Bundle/LogBridgeBundle/EventDispatcher/LogRequestListener.php @@ -1,47 +1,38 @@ contentFormatter = $contentFormatter; - $this->logger = null; - $this->matcher = null; } - /** - * onKernelTerminate - * - * @param FilterResponseEvent $event Event - */ - public function onKernelTerminate($event) + public function onKernelTerminate(ResponseEvent $event): void { + if ($this->matcher === null) { + return; + } + $request = $event->getRequest(); $response = $event->getResponse(); - $route = $request->get('_route'); + + /** @var string $route */ + $route = $request->get('_route', ''); $method = $request->getMethod(); $status = $response->getStatusCode(); $level = $this->matcher->getLevel($route, $method, $status); @@ -55,28 +46,14 @@ public function onKernelTerminate($event) } } - /** - * setLogger - * - * @param LoggerInterface $logger Logger - * - * @return LogRequestListener - */ - public function setLogger(LoggerInterface $logger) + public function setLogger(LoggerInterface $logger): self { $this->logger = $logger; return $this; } - /** - * setMatcher - * - * @param MatcherInterface $matcher Matcher - * - * @return LogRequestListener - */ - public function setMatcher(MatcherInterface $matcher) + public function setMatcher(MatcherInterface $matcher): self { $this->matcher = $matcher; diff --git a/src/M6Web/Bundle/LogBridgeBundle/Formatter/DefaultFormatter.php b/src/M6Web/Bundle/LogBridgeBundle/Formatter/DefaultFormatter.php index 2d82eae..5c7c260 100644 --- a/src/M6Web/Bundle/LogBridgeBundle/Formatter/DefaultFormatter.php +++ b/src/M6Web/Bundle/LogBridgeBundle/Formatter/DefaultFormatter.php @@ -1,5 +1,7 @@ environment = $environment; - $this->ignoreHeaders = $ignoreHeaders; - $this->prefixKey = $prefixKey; - $this->tokenStorage = null; + public function __construct( + protected string $environment, + protected array $ignoreHeaders = [], + protected string $prefixKey = '' + ) { } /** * Format parameters as tree * - * @return string + * @param array $parameters */ - protected function formatParameters(array $parameters) + protected function formatParameters(array $parameters): string { $tree = new \RecursiveTreeIterator(new \RecursiveArrayIterator($parameters)); $tree->setPrefixPart(\RecursiveTreeIterator::PREFIX_LEFT, ' '); @@ -62,20 +53,15 @@ protected function formatParameters(array $parameters) /** * getLogContent - * - * @param Request $request Request service - * @param Response $response Response service - * @param array $options Request options - * - * @return string */ - public function getLogContent(Request $request, Response $response, array $options) + public function getLogContent(Request $request, Response $response, array $options): string { $requestHeaders = $this->getFilteredHeaders($request); $requestContent = "Request\n------------------------\n"; - foreach ($requestHeaders as $name => $values) { - $requestContent .= str_pad($name, 20, ' ', STR_PAD_RIGHT).': '.$values[0]."\n"; + foreach ($requestHeaders as $name => $headerValue) { + $value = is_array($headerValue) ? $headerValue[0] : $headerValue; + $requestContent .= str_pad((string) $name, 20, ' ', STR_PAD_RIGHT).': '.$value."\n"; } $responseContent = sprintf( @@ -110,17 +96,9 @@ public function getLogContent(Request $request, Response $response, array $optio ); } - /** - * getLogContext - * - * @param Request $request Request service - * @param Response $response Response service - * @param array $options Request options - * - * @return array - */ - public function getLogContext(Request $request, Response $response, array $options) + public function getLogContext(Request $request, Response $response, array $options): array { + /** @var string $route */ $route = $request->get('_route'); $method = $request->getMethod(); $status = $response->getStatusCode(); @@ -142,19 +120,14 @@ public function getLogContext(Request $request, Response $response, array $optio return $context; } - /** - * getUsername - * - * @return string - */ - protected function getUsername() + protected function getUsername(): string { if (!$this->tokenStorage || !$token = $this->tokenStorage->getToken()) { return ''; } // compatibility Symfony < 6 - if (method_exists('Symfony\Component\Security\Core\Authentication\Token\TokenInterface', 'getUsername')) { + if (method_exists($token, 'getUsername')) { return $token->getUsername(); } @@ -162,25 +135,14 @@ protected function getUsername() } /** - * getFilteredHeaders - * - * @param Request $request Request service - * - * @return array + * @return array|string|null> */ - protected function getFilteredHeaders(Request $request) + protected function getFilteredHeaders(Request $request): array { return array_diff_key($request->headers->all(), array_flip($this->ignoreHeaders)); } - /** - * setContext - * - * @param TokenStorageInterface $tokenStorage User security token storage - * - * @return LogRequestListener - */ - public function setTokenStorage(TokenStorageInterface $tokenStorage) + public function setTokenStorage(TokenStorageInterface $tokenStorage): self { $this->tokenStorage = $tokenStorage; @@ -188,13 +150,9 @@ public function setTokenStorage(TokenStorageInterface $tokenStorage) } /** - * setIgnoreHeaders - * - * @param array $ignoreHeaders Array list of ignore header info - * - * @return $this + * @param string[] $ignoreHeaders */ - public function setIgnoreHeaders(array $ignoreHeaders) + public function setIgnoreHeaders(array $ignoreHeaders): self { $this->ignoreHeaders = $ignoreHeaders; @@ -202,33 +160,21 @@ public function setIgnoreHeaders(array $ignoreHeaders) } /** - * getIgnoreHeaders - * - * @return array + * @return string[] */ - public function getIgnoreHeaders() + public function getIgnoreHeaders(): array { return $this->ignoreHeaders; } - /** - * setPrefixKey - * - * @param string $prefixKey Prefix key - * - * @return $this - */ - public function setPrefixKey($prefixKey) + public function setPrefixKey(string $prefixKey): self { $this->prefixKey = $prefixKey; + + return $this; } - /** - * getPrefixKey - * - * @return string - */ - public function getPrefixKey() + public function getPrefixKey(): string { return $this->prefixKey; } diff --git a/src/M6Web/Bundle/LogBridgeBundle/Formatter/ExceptionFormatter.php b/src/M6Web/Bundle/LogBridgeBundle/Formatter/ExceptionFormatter.php index 37d459a..c849549 100644 --- a/src/M6Web/Bundle/LogBridgeBundle/Formatter/ExceptionFormatter.php +++ b/src/M6Web/Bundle/LogBridgeBundle/Formatter/ExceptionFormatter.php @@ -1,5 +1,7 @@ requestExceptionAttribute = $requestExceptionAttribute; } - /** - * Overload getLogContent to add details about the exception. - * - * @param Request $request Request service - * @param Response $response Response service - * @param array $options Request options - * - * @return string - */ - public function getLogContent(Request $request, Response $response, array $options) + public function getLogContent(Request $request, Response $response, array $options): string { $logContent = parent::getLogContent($request, $response, $options); if ($request->attributes->has($this->requestExceptionAttribute)) { + /** @var \Throwable $exception */ $exception = $request->attributes->get($this->requestExceptionAttribute); $logContent .= $this->getExceptionTrace($exception); @@ -43,12 +33,7 @@ public function getLogContent(Request $request, Response $response, array $optio return $logContent; } - /** - * @param int $level - * - * @return string - */ - protected function getExceptionTrace(\Throwable $exception, $level = 1) + protected function getExceptionTrace(\Throwable $exception, int $level = 1): string { $exceptionTrace = $this->formatException($exception, $level); @@ -59,15 +44,10 @@ protected function getExceptionTrace(\Throwable $exception, $level = 1) return $exceptionTrace; } - /** - * @param int $level - * - * @return string - */ - protected function formatException(\Throwable $exception, $level = 1) + protected function formatException(\Throwable $exception, int $level = 1): string { return - "\nException class : \n------------------------\n".get_class($exception)."\n". + "\nException class : \n------------------------\n".$exception::class."\n". "\nException message ($level) :\n------------------------\n".$exception->getMessage()."\n". "\nException trace ($level) :\n------------------------\n".$exception->getTraceAsString()."\n"; } diff --git a/src/M6Web/Bundle/LogBridgeBundle/Formatter/ExceptionFormatterInterface.php b/src/M6Web/Bundle/LogBridgeBundle/Formatter/ExceptionFormatterInterface.php index 5031e81..c007f74 100644 --- a/src/M6Web/Bundle/LogBridgeBundle/Formatter/ExceptionFormatterInterface.php +++ b/src/M6Web/Bundle/LogBridgeBundle/Formatter/ExceptionFormatterInterface.php @@ -1,5 +1,7 @@ $options */ - public function getLogContent(Request $request, Response $response, array $options); + public function getLogContent(Request $request, Response $response, array $options): string; /** - * @param Request $request Request service - * @param Response $response Response service - * @param array $options Request options + * @param array $options * - * @return array + * @return array */ - public function getLogContext(Request $request, Response $response, array $options); + public function getLogContext(Request $request, Response $response, array $options): array; } diff --git a/src/M6Web/Bundle/LogBridgeBundle/Logger/Logger.php b/src/M6Web/Bundle/LogBridgeBundle/Logger/Logger.php index 08eb8ea..d6b96e2 100644 --- a/src/M6Web/Bundle/LogBridgeBundle/Logger/Logger.php +++ b/src/M6Web/Bundle/LogBridgeBundle/Logger/Logger.php @@ -1,4 +1,6 @@ logger = $logger; } /** diff --git a/src/M6Web/Bundle/LogBridgeBundle/M6WebLogBridgeBundle.php b/src/M6Web/Bundle/LogBridgeBundle/M6WebLogBridgeBundle.php index ad2bbbe..2055ed1 100644 --- a/src/M6Web/Bundle/LogBridgeBundle/M6WebLogBridgeBundle.php +++ b/src/M6Web/Bundle/LogBridgeBundle/M6WebLogBridgeBundle.php @@ -1,5 +1,7 @@ */ + private string $matcherClassName; - /** @var string */ - private $matcherClassName; + private ?MatcherInterface $matcher = null; - /** @var MatcherInterface */ - private $matcher; - - /** @var array */ - private $filters; - - /** @var array */ - private $activeFilters; - - /** - * __construct - * - * @param StatusTypeManager $statusTypeManager Status type manager - * @param array $filters Filters - * @param array $activeFilters Active Filters - * @param string $environment Environment name - */ - public function __construct(StatusTypeManager $statusTypeManager, array $filters, array $activeFilters, $environment) - { - $this->statusTypeManager = $statusTypeManager; - $this->filters = $filters; - $this->activeFilters = $activeFilters; - $this->environment = $environment; - $this->dispatcher = null; - $this->configParser = null; - $this->debug = false; - $this->cacheDir = ''; - $this->matcherClassName = ''; - $this->matcher = null; + public function __construct( + private StatusTypeManager $statusTypeManager, + private array $filters, + private array $activeFilters + ) { } - /** - * buildMatcherCache - * - * @return string - */ - protected function buildMatcherCache() + protected function buildMatcherCache(): string { + $configs = []; $configs['filters'] = $this->filters; $configs['active_filters'] = $this->activeFilters; $configuration = $this->configParser->parse($configs); - $dumper = new Dumper\PhpMatcherDumper($this->statusTypeManager, $this->environment); + $dumper = new Dumper\PhpMatcherDumper($this->statusTypeManager); $options = []; - if ($this->matcherClassName) { + if (isset($this->matcherClassName)) { $options['class'] = $this->getMatcherClassName(); } return $dumper->dump($configuration, $options); } - /** - * getMatcher - * - * @return MatcherInterface - */ - public function getMatcher() + public function getMatcher(): MatcherInterface { - if (!$this->matcher) { + if (!isset($this->matcher)) { $matcherCache = new ConfigCache($this->getAbsoluteCachePath(), $this->isDebug()); if (!$matcherCache->isFresh()) { @@ -112,24 +67,14 @@ public function getMatcher() } /** - * getAbsoluteCachePath * Absolute path matcher class - * - * @return string */ - public function getAbsoluteCachePath() + public function getAbsoluteCachePath(): string { return sprintf('%s/%s.php', $this->getCacheDir(), $this->getMatcherClassName()); } - /** - * isDebug - * - * @param bool $debug Debug - * - * @return bool - */ - public function isDebug($debug = null) + public function isDebug(bool $debug = null): bool { if (is_bool($debug)) { $this->debug = $debug; @@ -138,50 +83,19 @@ public function isDebug($debug = null) return $this->debug; } - /** - * setCacheDir - * - * @param string $cacheDir cache directory path - * - * @return Builder - */ - public function setCacheDir($cacheDir) + public function setCacheDir(string $cacheDir): self { $this->cacheDir = $cacheDir; return $this; } - /** - * getCacheDir - * - * @return string - */ - public function getCacheDir() + public function getCacheDir(): string { return $this->cacheDir; } - /** - * setDispatcher - * - * @return Builder - */ - public function setDispatcher(EventDispatcherInterface $dispatcher) - { - $this->dispatcher = $dispatcher; - - return $this; - } - - /** - * setConfigParser - * - * @param Loader $configParser Config loader - * - * @return Builder - */ - public function setConfigParser(ConfigParser $configParser) + public function setConfigParser(ConfigParser $configParser): self { $this->configParser = $configParser; @@ -189,13 +103,9 @@ public function setConfigParser(ConfigParser $configParser) } /** - * setMatcherClassName - * - * @param string $matcherClassName Matcher class name - * - * @return Builder + * @param class-string $matcherClassName */ - public function setMatcherClassName($matcherClassName) + public function setMatcherClassName(string $matcherClassName): self { $this->matcherClassName = $matcherClassName; @@ -203,11 +113,9 @@ public function setMatcherClassName($matcherClassName) } /** - * getMatcherClassName - * - * @return string + * @return class-string */ - public function getMatcherClassName() + public function getMatcherClassName(): string { return $this->matcherClassName; } diff --git a/src/M6Web/Bundle/LogBridgeBundle/Matcher/BuilderInterface.php b/src/M6Web/Bundle/LogBridgeBundle/Matcher/BuilderInterface.php index b817e1c..ef15c2c 100644 --- a/src/M6Web/Bundle/LogBridgeBundle/Matcher/BuilderInterface.php +++ b/src/M6Web/Bundle/LogBridgeBundle/Matcher/BuilderInterface.php @@ -1,5 +1,7 @@ $filters + * @param string[] $activeFilters */ - public function __construct(StatusTypeManager $statusTypeManager, array $filters, array $activeFilters, $environment); + public function __construct(StatusTypeManager $statusTypeManager, array $filters, array $activeFilters); - /** - * getMatcher - * - * @return MatcherInterface - */ - public function getMatcher(); + public function getMatcher(): MatcherInterface; } diff --git a/src/M6Web/Bundle/LogBridgeBundle/Matcher/Dumper/PhpMatcherDumper.php b/src/M6Web/Bundle/LogBridgeBundle/Matcher/Dumper/PhpMatcherDumper.php index 59a7a13..be341f0 100644 --- a/src/M6Web/Bundle/LogBridgeBundle/Matcher/Dumper/PhpMatcherDumper.php +++ b/src/M6Web/Bundle/LogBridgeBundle/Matcher/Dumper/PhpMatcherDumper.php @@ -1,5 +1,7 @@ statusTypeManager = $statusTypeManager; } /** - * dump - * - * @return string + * @param array $options */ - public function dump(Configuration $configuration, array $options = []) + public function dump(Configuration $configuration, array $options = []): string { $options = array_replace([ 'class' => 'LogBridgeMatcher', - 'interface' => 'M6Web\\Bundle\\LogBridgeBundle\\Matcher\\MatcherInterface', + 'interface' => \M6Web\Bundle\LogBridgeBundle\Matcher\MatcherInterface::class, 'default_level' => LogLevel::INFO, ], $options); @@ -54,16 +50,7 @@ class {$options['class']} implements {$options['interface']} { {$this->generateMatchList($configuration)} - /** - * getPositiveMatcher - * - * @param string \$route Route name - * @param string \$method Method name - * @param integer \$status Http code status - * - * @return array - */ - private function getPositiveMatcher(\$route, \$method, \$status) + private function getPositiveMatcher(string \$route, string \$method, int \$status): array { return [ [\$route, \$method, \$status], @@ -77,48 +64,17 @@ private function getPositiveMatcher(\$route, \$method, \$status) ]; } - /** - * match - * - * @param string \$route Route name - * @param string \$method Method name - * @param integer \$status Http code status - * - * @return boolean - */ - public function match(\$route, \$method, \$status) + public function match(string \$route, string \$method, int \$status): bool { - if (\$filterKey = \$this->getMatchFilterKey(\$route, \$method, \$status)) { - return true; - } - - return false; + return \$this->getMatchFilterKey(\$route, \$method, \$status) !== null; } - /** - * generate filter key - * - * @param string \$route Route name - * @param string \$method Method name - * @param integer \$status Http code status - * - * @return string - */ - public function generateFilterKey(\$route, \$method, \$status) + public function generateFilterKey(string \$route, string \$method, string \$status): string { return sprintf('%s.%s.%s', \$route, \$method, \$status); } - /** - * Get filter options - * - * @param string \$route Route name - * @param string \$method Method name - * @param integer \$status Http code status - * - * @return array - */ - public function getOptions(\$route, \$method, \$status) + public function getOptions(string \$route, string \$method, int \$status): array { if (\$filterKey = \$this->getMatchFilterKey(\$route, \$method, \$status)) { return \$this->filters[\$filterKey]['options']; @@ -127,16 +83,7 @@ public function getOptions(\$route, \$method, \$status) return []; } - /** - * Get filter level log - * - * @param string \$route Route name - * @param string \$method Method name - * @param integer \$status Http code status - * - * @return string - */ - public function getLevel(\$route, \$method, \$status) + public function getLevel(string \$route, string \$method, int \$status): string { if (\$filterKey = \$this->getMatchFilterKey(\$route, \$method, \$status)) { return \$this->filters[\$filterKey]['level']; @@ -145,35 +92,18 @@ public function getLevel(\$route, \$method, \$status) return '{$options['default_level']}'; } - /** - * addFilter - * - * @param string \$filterKey Filter key - * @param string \$level Filter Log level - * @param array \$options Filter options - * - * @return MatcherInterface - */ - public function addFilter(\$filterKey, \$level = '{$options['default_level']}', array \$options = []) + public function addFilter(string \$filter, string \$level = '{$options['default_level']}', array \$options = []): self { - if (!\$this->hasFilter(\$filterKey)) { - \$this->filters[\$filterKey] = []; - \$this->filters[\$filterKey]['options'] = \$options; - \$this->filters[\$filterKey]['level'] = \$level; + if (!\$this->hasFilter(\$filter)) { + \$this->filters[\$filter] = []; + \$this->filters[\$filter]['options'] = \$options; + \$this->filters[\$filter]['level'] = \$level; } return \$this; } - /** - * setFilters - * - * @param array \$filters Filter list - * @param boolean \$overwrite Overwrite current filter - * - * @return MatcherInterface - */ - public function setFilters(array \$filters, \$overwrite = false) + public function setFilters(array \$filters, bool \$overwrite = false): self { if (\$overwrite) { \$this->filters = \$filters; @@ -195,38 +125,17 @@ public function setFilters(array \$filters, \$overwrite = false) return \$this; } - /** - * getFilters - * - * @return array - */ - public function getFilters() + public function getFilters(): array { return \$this->filters; } - /** - * hasFilter - * - * @param string \$filterKey Filter key - * - * @return boolean - */ - public function hasFilter(\$filterKey) + public function hasFilter(string \$filter): bool { - return array_key_exists(\$filterKey, \$this->filters); + return array_key_exists(\$filter, \$this->filters); } - /** - * get an filter key matched with arguments - * - * @param string \$route Route name - * @param string \$method Method name - * @param integer \$status Http code status - * - * @return bool|string - */ - public function getMatchFilterKey(\$route, \$method, \$status) + public function getMatchFilterKey(string \$route, string \$method, int \$status): ?string { if (!empty(\$this->filters)) { foreach (\$this->getPositiveMatcher(\$route, \$method, \$status) as \$rms) { @@ -237,19 +146,14 @@ public function getMatchFilterKey(\$route, \$method, \$status) } } - return false; + return null; } } EOF; } - /** - * generateMatchList - * - * @return string - */ - private function generateMatchList(Configuration $configuration) + private function generateMatchList(Configuration $configuration): string { $filters = $this->compile($configuration); $code = "[\n"; @@ -292,41 +196,44 @@ private function generateMatchList(Configuration $configuration) return << + * }> */ - private function compile(Configuration $configuration) + private function compile(Configuration $configuration): array { - $compiledFilters = $this->compileNeededFilters( + $filters = $configuration->getFilters(); + if ($filters === null) { + return []; + } + + return $this->compileNeededFilters( $configuration->getActiveFilters(), - $configuration->getFilters() + $filters ); - - return $compiledFilters; } /** - * @param array $activeFilters List of active filters - * @param FilterCollection $filters Filters + * @param string[]|null $activeFilters * - * @return array + * @return array + * }> */ - private function compileNeededFilters($activeFilters, FilterCollection $filters) + private function compileNeededFilters(?array $activeFilters, FilterCollection $filters): array { $compiled = []; - if (is_null($activeFilters)) { + if ($activeFilters === null) { foreach ($filters as $filter) { $compiled = array_merge($compiled, $this->compileFilter($filter)); } @@ -342,22 +249,20 @@ private function compileNeededFilters($activeFilters, FilterCollection $filters) } /** - * compileFilter - * - * @param Filter $filter Filter - * - * @internal param string $prefix Prefix key - * - * @return array + * @return array + * }> */ - private function compileFilter(Filter $filter) + private function compileFilter(Filter $filter): array { $compiledKeys = []; $compiled = []; + /** @var string $prefix */ $prefix = is_null($filter->getRoute()) ? 'all' : $filter->getRoute(); if (empty($filter->getMethod())) { - $prefix = sprintf('%s.all', $prefix, $filter->getMethod()); + $prefix = sprintf('%s.all', $prefix); $compiledKeys = $this->compileFilterStatus($prefix, $filter); } else { foreach ($filter->getMethod() as $method) { @@ -375,16 +280,12 @@ private function compileFilter(Filter $filter) } /** - * compileFilterStatus - * - * @param string $prefix Prefix key - * @param Filter $filter Filter - * - * @return array + * @return string[] */ - private function compileFilterStatus($prefix, $filter) + private function compileFilterStatus(string $prefix, Filter $filter): array { $compiled = []; + /** @var string[]|null $filterStatusList */ $filterStatusList = $filter->getStatus(); if (is_null($filterStatusList)) { @@ -399,13 +300,11 @@ private function compileFilterStatus($prefix, $filter) } /** - * parseStatus - * - * @throws \Exception status not allowed in log bridge configuration filters + * @param string[] $filterStatusList * - * @return array + * @return string[]|int[] */ - private function parseStatus(array $filterStatusList) + private function parseStatus(array $filterStatusList): array { $statusList = []; $statusTypes = $this->statusTypeManager->getTypes(); @@ -419,7 +318,7 @@ private function parseStatus(array $filterStatusList) if ($statusType->isExclude($value)) { $statusList = array_diff($statusList, $statusType->getStatus($value)); } else { - $statusList = array_merge($statusList, $statusType->getStatus($value)); + $statusList = [...$statusList, ...$statusType->getStatus($value)]; } break; @@ -431,8 +330,6 @@ private function parseStatus(array $filterStatusList) } } - $statusList = array_unique($statusList, SORT_NUMERIC); - - return $statusList; + return array_unique($statusList, SORT_NUMERIC); } } diff --git a/src/M6Web/Bundle/LogBridgeBundle/Matcher/MatcherInterface.php b/src/M6Web/Bundle/LogBridgeBundle/Matcher/MatcherInterface.php index a55fdef..824b02e 100644 --- a/src/M6Web/Bundle/LogBridgeBundle/Matcher/MatcherInterface.php +++ b/src/M6Web/Bundle/LogBridgeBundle/Matcher/MatcherInterface.php @@ -1,5 +1,7 @@ $options */ - public function addFilter($filter, $level = LogLevel::INFO, array $options = []); + public function addFilter(string $filter, string $level = LogLevel::INFO, array $options = []): self; /** - * setFilters - * - * @param array $filters Filter list - * @param bool $overwrite Overwrite current filter - * - * @return MatcherInterface + * @param array + * }> $filters */ - public function setFilters(array $filters, $overwrite = false); + public function setFilters(array $filters, bool $overwrite = false): self; /** - * getFilters - * - * @return array + * @return array + * }> */ - public function getFilters(); + public function getFilters(): array; - /** - * hasFilter - * - * @param string $filter Filter - * - * @return bool - */ - public function hasFilter($filter); + public function hasFilter(string $filter): bool; - /** - * Get filter level log - * - * @param string $route Route name - * @param string $method Method name - * @param int $status Http code status - * - * @return string - */ - public function getLevel($route, $method, $status); + public function getLevel(string $route, string $method, int $status): string; /** - * get options - * - * @param string $route Route name - * @param string $method Method name - * @param int $status Http code status - * - * @return array + * @return array */ - public function getOptions($route, $method, $status); + public function getOptions(string $route, string $method, int $status): array; /** - * get an filter key matched with arguments - * - * @param string $route Route name - * @param string $method Method name - * @param int $status Http code status - * - * @return bool|string + * get a filter key matched with arguments */ - public function getMatchFilterKey($route, $method, $status); + public function getMatchFilterKey(string $route, string $method, int $status): ?string; } diff --git a/src/M6Web/Bundle/LogBridgeBundle/Matcher/MatcherProxy.php b/src/M6Web/Bundle/LogBridgeBundle/Matcher/MatcherProxy.php index fa87771..8a3960b 100644 --- a/src/M6Web/Bundle/LogBridgeBundle/Matcher/MatcherProxy.php +++ b/src/M6Web/Bundle/LogBridgeBundle/Matcher/MatcherProxy.php @@ -1,5 +1,7 @@ builder = $builder; $this->matcher = $builder->getMatcher(); } - /** - * match - * - * @param string $route Route name - * @param string $method Method name - * @param int $status Http code status - * - * @return bool - */ - public function match($route, $method, $status) + public function match(string $route, string $method, int $status): bool { return $this->matcher->match($route, $method, $status); } - /** - * Get filter level log - * - * @param string $route Route name - * @param string $method Method name - * @param int $status Http code status - * - * @return string - */ - public function getLevel($route, $method, $status) + public function getLevel(string $route, string $method, int $status): string { return $this->matcher->getLevel($route, $method, $status); } - /** - * get options - * - * @param string $route Route name - * @param string $method Method name - * @param int $status Http code status - * - * @return array - */ - public function getOptions($route, $method, $status) + public function getOptions(string $route, string $method, int $status): array { return $this->matcher->getOptions($route, $method, $status); } - /** - * addFilter - * - * @param string $filter Filter - * @param string $level Log level - * @param array $options Options - * - * @return MatcherProxy - */ - public function addFilter($filter, $level = LogLevel::INFO, array $options = []) + public function addFilter(string $filter, string $level = LogLevel::INFO, array $options = []): self { $this->matcher->addFilter($filter, $level, $options); return $this; } - /** - * setFilters - * - * @param array $filters Filter list - * @param bool $overwrite need overwrite filter - * - * @return MatcherProxy - */ - public function setFilters(array $filters, $overwrite = false) + public function setFilters(array $filters, bool $overwrite = false): self { $this->matcher->setFilters($filters, $overwrite); return $this; } - /** - * getFilters - * - * @return array - */ - public function getFilters() + public function getFilters(): array { return $this->matcher->getFilters(); } - /** - * hasFilter - * - * @param string $filter Filter - * - * @return bool - */ - public function hasFilter($filter) + public function hasFilter(string $filter): bool { return $this->matcher->hasFilter($filter); } - /** - * getMatcher - * - * @return MatcherInterface - */ - public function getMatcher() + public function getMatcher(): MatcherInterface { return $this->matcher; } - /** - * getBuilder - * - * @return BuilderInterface - */ - public function getBuilder() + public function getBuilder(): BuilderInterface { return $this->builder; } /** - * get an filter key matched with arguments - * - * @param string $route Route name - * @param string $method Method name - * @param int $status Http code status - * - * @return BuilderInterface + * get a filter key matched with arguments */ - public function getMatchFilterKey($route, $method, $status) + public function getMatchFilterKey(string $route, string $method, int $status): ?string { return $this->matcher->getMatchFilterKey($route, $method, $status); } diff --git a/src/M6Web/Bundle/LogBridgeBundle/Matcher/Status/Type/AbstractType.php b/src/M6Web/Bundle/LogBridgeBundle/Matcher/Status/Type/AbstractType.php index ac02b17..956ee63 100644 --- a/src/M6Web/Bundle/LogBridgeBundle/Matcher/Status/Type/AbstractType.php +++ b/src/M6Web/Bundle/LogBridgeBundle/Matcher/Status/Type/AbstractType.php @@ -1,5 +1,7 @@ getPattern(), $config); + return preg_match($this->getPattern(), $config) === 1; } /** * Get status list * - * @param string $config - * * @throws \Exception transform method must be return an array - * - * @return array */ - final public function getStatus($config) + final public function getStatus(string $config): array { if ($this->isExclude($config)) { $config = substr($config, 1); } - $status = $this->transform($config); - - if (!is_array($status)) { - throw new \Exception(sprintf('"transform" method must be return an array in class "%s"', get_class($this)), 500); - } - - return $status; + return $this->transform($config); } /** * Match with config status - * - * @param string $config - * - * @return bool */ - final public function isExclude($config) + final public function isExclude(string $config): bool { - if (strpos($config, '!') !== false) { - return true; - } - - return false; + return str_contains($config, '!'); } - /** - * getPattern - * - * @return string - */ - abstract protected function getPattern(); + abstract protected function getPattern(): string; /** * Transform config to status list * - * @param string $config - * - * @return array + * @return array */ - abstract protected function transform($config); + abstract protected function transform(string $config): array; } diff --git a/src/M6Web/Bundle/LogBridgeBundle/Matcher/Status/Type/CompleteType.php b/src/M6Web/Bundle/LogBridgeBundle/Matcher/Status/Type/CompleteType.php index b705802..ea412e0 100644 --- a/src/M6Web/Bundle/LogBridgeBundle/Matcher/Status/Type/CompleteType.php +++ b/src/M6Web/Bundle/LogBridgeBundle/Matcher/Status/Type/CompleteType.php @@ -1,43 +1,36 @@ + */ + public function getStatus(string $config): array; + + /** + * Match with config status */ - public function getStatus($config); + public function isExclude(string $config): bool; } diff --git a/src/M6Web/Bundle/LogBridgeBundle/Matcher/Status/Type/UpperType.php b/src/M6Web/Bundle/LogBridgeBundle/Matcher/Status/Type/UpperType.php index 90a9ed3..726f30c 100644 --- a/src/M6Web/Bundle/LogBridgeBundle/Matcher/Status/Type/UpperType.php +++ b/src/M6Web/Bundle/LogBridgeBundle/Matcher/Status/Type/UpperType.php @@ -1,32 +1,25 @@ , TypeInterface> */ + protected array $types = []; /** - * Get types - * - * @return array + * @return array, TypeInterface> */ - public function getTypes() + public function getTypes(): array { return $this->types; } /** * Add a type - * - * @return $this */ - public function addType(TypeInterface $type) + public function addType(TypeInterface $type): self { if (!$this->isType($type)) { - $typeClass = get_class($type); + $typeClass = $type::class; $this->types[$typeClass] = $type; } @@ -40,14 +38,12 @@ public function addType(TypeInterface $type) /** * Remove a type - * - * @return $this */ - public function removeType(TypeInterface $typeToRemove) + public function removeType(TypeInterface $typeToRemove): self { - $namespaceTypeToRemove = get_class($typeToRemove); + $namespaceTypeToRemove = $typeToRemove::class; foreach ($this->types as $key => $type) { - if ($namespaceTypeToRemove === get_class($type)) { + if ($namespaceTypeToRemove === $type::class) { unset($this->types[$key]); } } @@ -57,14 +53,12 @@ public function removeType(TypeInterface $typeToRemove) /** * Check if type pass is already record as type - * - * @return bool */ - protected function isType(TypeInterface $typeToCheck) + protected function isType(TypeInterface $typeToCheck): bool { - $namespaceTypeToCheck = get_class($typeToCheck); - foreach ($this->types as $key => $type) { - if ($namespaceTypeToCheck === get_class($type)) { + $namespaceTypeToCheck = $typeToCheck::class; + foreach ($this->types as $type) { + if ($namespaceTypeToCheck === $type::class) { return true; } } diff --git a/src/M6Web/Bundle/LogBridgeBundle/Resources/config/services.yml b/src/M6Web/Bundle/LogBridgeBundle/Resources/config/services.yml index a5935b7..5f334e3 100644 --- a/src/M6Web/Bundle/LogBridgeBundle/Resources/config/services.yml +++ b/src/M6Web/Bundle/LogBridgeBundle/Resources/config/services.yml @@ -40,7 +40,6 @@ services: - "%m6web_log_bridge.active_filters%" - "%kernel.environment%" calls: - - [ setDispatcher, [ "@event_dispatcher" ] ] - [ setConfigParser, [ "@m6web_log_bridge.config_parser" ] ] - [ setCacheDir, [ "%kernel.cache_dir%" ] ] - [ isDebug, [ "%kernel.debug%" ] ] diff --git a/src/M6Web/Bundle/LogBridgeBundle/Tests/Fixtures/Matcher/Status/Type/Sample1Type.php b/src/M6Web/Bundle/LogBridgeBundle/Tests/Fixtures/Matcher/Status/Type/Sample1Type.php index 83e4183..b4dd495 100644 --- a/src/M6Web/Bundle/LogBridgeBundle/Tests/Fixtures/Matcher/Status/Type/Sample1Type.php +++ b/src/M6Web/Bundle/LogBridgeBundle/Tests/Fixtures/Matcher/Status/Type/Sample1Type.php @@ -3,31 +3,21 @@ namespace M6Web\Bundle\LogBridgeBundle\Tests\Fixtures\Matcher\Status\Type; use M6Web\Bundle\LogBridgeBundle\Matcher\Status\Type\AbstractType; -use M6Web\Bundle\LogBridgeBundle\Matcher\Status\Type\TypeInterface; /** * Class Sample1Type */ -class Sample1Type extends AbstractType implements TypeInterface +class Sample1Type extends AbstractType { - /** - * getPattern - * - * @return string - */ - protected function getPattern() + protected function getPattern(): string { return '/^!?[\d]{3}$/'; } /** * Transform config to status list - * - * @param string $config - * - * @return array */ - public function transform($config) + public function transform(string $config): array { return [$config]; } diff --git a/src/M6Web/Bundle/LogBridgeBundle/Tests/Fixtures/Matcher/Status/Type/Sample2Type.php b/src/M6Web/Bundle/LogBridgeBundle/Tests/Fixtures/Matcher/Status/Type/Sample2Type.php index 8909c39..b1d0638 100644 --- a/src/M6Web/Bundle/LogBridgeBundle/Tests/Fixtures/Matcher/Status/Type/Sample2Type.php +++ b/src/M6Web/Bundle/LogBridgeBundle/Tests/Fixtures/Matcher/Status/Type/Sample2Type.php @@ -3,31 +3,21 @@ namespace M6Web\Bundle\LogBridgeBundle\Tests\Fixtures\Matcher\Status\Type; use M6Web\Bundle\LogBridgeBundle\Matcher\Status\Type\AbstractType; -use M6Web\Bundle\LogBridgeBundle\Matcher\Status\Type\TypeInterface; /** * Class Sample2Type */ -class Sample2Type extends AbstractType implements TypeInterface +class Sample2Type extends AbstractType { - /** - * getPattern - * - * @return string - */ - protected function getPattern() + protected function getPattern(): string { return '/^!?[\d]{3}$/'; } /** * Transform config to status list - * - * @param string $config - * - * @return array */ - protected function transform($config) + protected function transform(string $config): array { return [$config]; } diff --git a/src/M6Web/Bundle/LogBridgeBundle/Tests/Fixtures/Matcher/Status/Type/Sample3Type.php b/src/M6Web/Bundle/LogBridgeBundle/Tests/Fixtures/Matcher/Status/Type/Sample3Type.php index a9b97d0..875ebf6 100644 --- a/src/M6Web/Bundle/LogBridgeBundle/Tests/Fixtures/Matcher/Status/Type/Sample3Type.php +++ b/src/M6Web/Bundle/LogBridgeBundle/Tests/Fixtures/Matcher/Status/Type/Sample3Type.php @@ -3,31 +3,21 @@ namespace M6Web\Bundle\LogBridgeBundle\Tests\Fixtures\Matcher\Status\Type; use M6Web\Bundle\LogBridgeBundle\Matcher\Status\Type\AbstractType; -use M6Web\Bundle\LogBridgeBundle\Matcher\Status\Type\TypeInterface; /** * Class Sample3Type */ -class Sample3Type extends AbstractType implements TypeInterface +class Sample3Type extends AbstractType { - /** - * getPattern - * - * @return string - */ - protected function getPattern() + protected function getPattern(): string { return '/^!?[\d]{3}$/'; } /** * Transform config to status list - * - * @param string $config - * - * @return array */ - protected function transform($config) + protected function transform(string $config): array { return [$config]; } diff --git a/src/M6Web/Bundle/LogBridgeBundle/Tests/Fixtures/Resources/cache/TestLogMatcher.php b/src/M6Web/Bundle/LogBridgeBundle/Tests/Fixtures/Resources/cache/TestLogMatcher.php deleted file mode 100644 index 59bac25..0000000 --- a/src/M6Web/Bundle/LogBridgeBundle/Tests/Fixtures/Resources/cache/TestLogMatcher.php +++ /dev/null @@ -1,789 +0,0 @@ - [ - 'options' => [ - 'response_body' => true, - 'post_parameters' => true, - ], - 'level' => 'info', - ], - 'get_clip.PUT.200' => [ - 'options' => [ - 'response_body' => true, - 'post_parameters' => true, - ], - 'level' => 'info', - ], - 'all.PUT.200' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.201' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.202' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.203' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.204' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.205' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.206' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.207' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.208' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.209' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.210' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.211' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.212' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.213' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.214' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.215' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.216' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.217' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.218' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.219' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.220' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.221' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.222' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.223' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.224' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.225' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.226' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.227' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.228' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.229' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.230' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.231' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.232' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.233' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.234' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.235' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.236' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.237' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.238' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.239' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.240' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.241' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.242' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.243' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.244' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.245' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.246' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.247' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.248' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.249' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.250' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.251' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.252' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.253' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.254' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.255' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.256' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.257' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.258' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.259' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.260' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.261' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.262' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.263' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.264' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.265' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.266' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.267' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.268' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.269' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.270' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.271' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.272' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.273' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.274' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.275' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.276' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.277' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.278' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.279' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.280' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.281' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.282' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.283' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.284' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.285' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.286' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.287' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.288' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.289' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.290' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.291' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.292' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.293' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.294' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.295' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.296' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.297' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.298' => [ - 'options' => [], - 'level' => 'info', - ], - 'all.PUT.299' => [ - 'options' => [], - 'level' => 'info', - ], - 'delete_clip.DELETE.450' => [ - 'options' => [], - 'level' => 'info', - ], - 'delete_clip.DELETE.451' => [ - 'options' => [], - 'level' => 'info', - ], - 'delete_clip.DELETE.459' => [ - 'options' => [], - 'level' => 'info', - ], - 'delete_clip.DELETE.460' => [ - 'options' => [], - 'level' => 'info', - ], - 'delete_clip.DELETE.461' => [ - 'options' => [], - 'level' => 'info', - ], - 'delete_clip.DELETE.462' => [ - 'options' => [], - 'level' => 'info', - ], - 'delete_clip.DELETE.463' => [ - 'options' => [], - 'level' => 'info', - ], - 'delete_clip.DELETE.464' => [ - 'options' => [], - 'level' => 'info', - ], - 'delete_clip.DELETE.465' => [ - 'options' => [], - 'level' => 'info', - ], - 'delete_clip.DELETE.466' => [ - 'options' => [], - 'level' => 'info', - ], - 'delete_clip.DELETE.467' => [ - 'options' => [], - 'level' => 'info', - ], - 'delete_clip.DELETE.468' => [ - 'options' => [], - 'level' => 'info', - ], - 'delete_clip.DELETE.469' => [ - 'options' => [], - 'level' => 'info', - ], - 'delete_clip.DELETE.470' => [ - 'options' => [], - 'level' => 'info', - ], - 'delete_clip.DELETE.471' => [ - 'options' => [], - 'level' => 'info', - ], - 'delete_clip.DELETE.472' => [ - 'options' => [], - 'level' => 'info', - ], - 'delete_clip.DELETE.473' => [ - 'options' => [], - 'level' => 'info', - ], - 'delete_clip.DELETE.474' => [ - 'options' => [], - 'level' => 'info', - ], - 'delete_clip.DELETE.475' => [ - 'options' => [], - 'level' => 'info', - ], - 'delete_clip.DELETE.476' => [ - 'options' => [], - 'level' => 'info', - ], - 'delete_clip.DELETE.477' => [ - 'options' => [], - 'level' => 'info', - ], - 'delete_clip.DELETE.478' => [ - 'options' => [], - 'level' => 'info', - ], - 'delete_clip.DELETE.479' => [ - 'options' => [], - 'level' => 'info', - ], - 'delete_clip.DELETE.480' => [ - 'options' => [], - 'level' => 'info', - ], - 'delete_clip.DELETE.481' => [ - 'options' => [], - 'level' => 'info', - ], - 'delete_clip.DELETE.482' => [ - 'options' => [], - 'level' => 'info', - ], - 'delete_clip.DELETE.483' => [ - 'options' => [], - 'level' => 'info', - ], - 'delete_clip.DELETE.484' => [ - 'options' => [], - 'level' => 'info', - ], - 'delete_clip.DELETE.485' => [ - 'options' => [], - 'level' => 'info', - ], - 'delete_clip.DELETE.486' => [ - 'options' => [], - 'level' => 'info', - ], - 'delete_clip.DELETE.487' => [ - 'options' => [], - 'level' => 'info', - ], - 'delete_clip.DELETE.488' => [ - 'options' => [], - 'level' => 'info', - ], - 'delete_clip.DELETE.489' => [ - 'options' => [], - 'level' => 'info', - ], - 'delete_clip.DELETE.490' => [ - 'options' => [], - 'level' => 'info', - ], - 'delete_clip.DELETE.491' => [ - 'options' => [], - 'level' => 'info', - ], - 'delete_clip.DELETE.492' => [ - 'options' => [], - 'level' => 'info', - ], - 'delete_clip.DELETE.493' => [ - 'options' => [], - 'level' => 'info', - ], - 'delete_clip.DELETE.494' => [ - 'options' => [], - 'level' => 'info', - ], - 'delete_clip.DELETE.495' => [ - 'options' => [], - 'level' => 'info', - ], - 'delete_clip.DELETE.496' => [ - 'options' => [], - 'level' => 'info', - ], - 'delete_clip.DELETE.497' => [ - 'options' => [], - 'level' => 'info', - ], - 'delete_clip.DELETE.498' => [ - 'options' => [], - 'level' => 'info', - ], - 'delete_clip.DELETE.499' => [ - 'options' => [], - 'level' => 'info', - ] - ]; - - /** - * getPositiveMatcher - * - * @param string $route Route name - * @param string $method Method name - * @param integer $status Http code status - * - * @return array - */ - private function getPositiveMatcher($route, $method, $status) - { - return [ - [$route, $method, $status], - [$route, $method, 'all'], - [$route, 'all', $status], - ['all', $method, $status], - [$route, 'all', 'all'], - ['all', 'all', $status], - ['all', $method, 'all'], - ['all', 'all', 'all'] - ]; - } - - /** - * match - * - * @param string $route Route name - * @param string $method Method name - * @param integer $status Http code status - * - * @return boolean - */ - public function match($route, $method, $status) - { - if ($filterKey = $this->getMatchFilterKey($route, $method, $status)) { - return true; - } - - return false; - } - - /** - * generate filter key - * - * @param string $route Route name - * @param string $method Method name - * @param integer $status Http code status - * - * @return string - */ - public function generateFilterKey($route, $method, $status) - { - return sprintf('%s.%s.%s', $route, $method, $status); - } - - /** - * Get filter options - * - * @param string $route Route name - * @param string $method Method name - * @param integer $status Http code status - * - * @return array - */ - public function getOptions($route, $method, $status) - { - if ($filterKey = $this->getMatchFilterKey($route, $method, $status)) { - return $this->filters[$filterKey]['options']; - } - - return []; - } - - /** - * Get filter level log - * - * @param string $route Route name - * @param string $method Method name - * @param integer $status Http code status - * - * @return string - */ - public function getLevel($route, $method, $status) - { - if ($filterKey = $this->getMatchFilterKey($route, $method, $status)) { - return $this->filters[$filterKey]['level']; - } - - return 'info'; - } - - /** - * addFilter - * - * @param string $filterKey Filter key - * @param string $level Filter Log level - * @param array $options Filter options - * - * @return MatcherInterface - */ - public function addFilter($filterKey, $level = 'info', array $options = []) - { - if (!$this->hasFilter($filterKey)) { - $this->filters[$filterKey] = []; - $this->filters[$filterKey]['options'] = $options; - $this->filters[$filterKey]['level'] = $level; - } - - return $this; - } - - /** - * setFilters - * - * @param array $filters Filter list - * @param boolean $overwrite Overwrite current filter - * - * @return MatcherInterface - */ - public function setFilters(array $filters, $overwrite = false) - { - if ($overwrite) { - $this->filters = $filters; - } else { - foreach ($filters as $filterKey => $filter) { - - if (!isset($filter['level'])) { - $filter['level'] = 'info'; - } - - if (!isset($filter['options'])) { - $filter['options'] = []; - } - - $this->addFilter($filterKey, $filter['level'], $filter['options']); - } - } - - return $this; - } - - /** - * getFilters - * - * @return array - */ - public function getFilters() - { - return $this->filters; - } - - /** - * hasFilter - * - * @param string $filterKey Filter key - * - * @return boolean - */ - public function hasFilter($filterKey) - { - return array_key_exists($filterKey, $this->filters); - } - - /** - * get an filter key matched with arguments - * - * @param string $route Route name - * @param string $method Method name - * @param integer $status Http code status - * - * @return bool|string - */ - public function getMatchFilterKey($route, $method, $status) - { - if (!empty($this->filters)) { - foreach ($this->getPositiveMatcher($route, $method, $status) as $rms) { - $filterKey = $this->generateFilterKey($rms[0], $rms[1], $rms[2]); - if ($this->hasFilter($filterKey)) { - return $filterKey; - } - } - } - - return false; - } -} diff --git a/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/BaseTest.php b/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/BaseTest.php index 11d4b41..d9b12b0 100644 --- a/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/BaseTest.php +++ b/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/BaseTest.php @@ -11,28 +11,24 @@ */ class BaseTest extends atoum { - protected function getMockedRouterCollection() + protected function getMockedRouterCollection(): object { $collection = new \mock\Symfony\Component\Routing\RouteCollection(); - $collection->getMockController()->get = function($name) { - return $name != 'invalid_route' ? new Route('/path') : null; - }; + $collection->getMockController()->get = fn($name) => $name != 'invalid_route' ? new Route('/path') : null; return $collection; } - protected function getMockedRouter() + protected function getMockedRouter(): object { $this->mockGenerator->orphanize('__construct'); $router = new \mock\Symfony\Component\Routing\Router(); $routerCollection = $this->getMockedRouterCollection(); $router->getMockController()->getRouteCollection = $routerCollection; - $router->getMockController()->generate = function($name, $parameters = [], $referenceType = false) { - return $name; - }; + $router->getMockController()->generate = fn($name, $parameters = [], $referenceType = false) => $name; - $router->getMockController()->match = function($pathinfo) { return []; }; + $router->getMockController()->match = fn($pathinfo) => []; $router->getMockController()->setContext = function(RequestContext $context) { return; }; $router->getMockController()->getContext = null; diff --git a/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Config/Configuration.php b/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Config/Configuration.php index 1ba9460..2362266 100644 --- a/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Config/Configuration.php +++ b/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Config/Configuration.php @@ -2,12 +2,12 @@ namespace M6Web\Bundle\LogBridgeBundle\Tests\Units\Config; -use atoum; +use atoum; use M6Web\Bundle\LogBridgeBundle\Config; class Configuration extends atoum { - private function getFilters() + private function getFilters(): Config\FilterCollection { $collection = new Config\FilterCollection(); @@ -15,20 +15,18 @@ private function getFilters() $filter ->setRoute('route_name') ->setMethod(['GET', 'POST']) - ->setStatus('all'); + ->setStatus(['all']); $collection->add($filter); - $filter = new Config\Filter('active_filters_two'); $filter ->setRoute('route_name_two') ->setMethod(['PUT', 'POST']) - ->setStatus('200'); + ->setStatus([200]); $collection->add($filter); - $filter = new Config\Filter('active_filters_three'); $filter ->setRoute('route_name_three') @@ -40,16 +38,16 @@ private function getFilters() return $collection; } - private function getActiveFilters() + private function getActiveFilters(): array { return [ 'active_filters_one', 'active_filters_two', 'active_filters_three' - ]; + ]; } - public function testConfiguration() + public function testConfiguration(): void { $activeFilters = $this->getActiveFilters(); @@ -59,15 +57,15 @@ public function testConfiguration() ->variable($configuration->getActiveFilters()) ->isNull() ->object($configuration->setActiveFilters($activeFilters)) - ->isInstanceOf('M6Web\Bundle\LogBridgeBundle\Config\Configuration') + ->isInstanceOf(\M6Web\Bundle\LogBridgeBundle\Config\Configuration::class) ->array($configuration->getActiveFilters()) ->hasSize(3) ->hasKeys(array_keys($activeFilters)) ->variable($configuration->getFilters()) ->isNull() ->object($configuration->setFilters($this->getFilters())) - ->isInstanceOf('M6Web\Bundle\LogBridgeBundle\Config\Configuration') + ->isInstanceOf(\M6Web\Bundle\LogBridgeBundle\Config\Configuration::class) ->object($collection = $configuration->getFilters()) - ->isInstanceOf('M6Web\Bundle\LogBridgeBundle\Config\FilterCollection'); + ->isInstanceOf(\M6Web\Bundle\LogBridgeBundle\Config\FilterCollection::class); } } diff --git a/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Config/Filter.php b/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Config/Filter.php index a2251a2..b3877ad 100644 --- a/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Config/Filter.php +++ b/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Config/Filter.php @@ -7,7 +7,7 @@ class Filter extends atoum { - public function testFilter() + public function testFilter(): void { $this ->if($filter = new Config\Filter('filter_name')) @@ -15,15 +15,15 @@ public function testFilter() ->string($filter->getName()) ->isEqualTo('filter_name') ->object($filter->setRoute('filter_route')) - ->isInstanceOf('M6Web\Bundle\LogBridgeBundle\Config\Filter') + ->isInstanceOf(\M6Web\Bundle\LogBridgeBundle\Config\Filter::class) ->string($filter->getRoute()) ->isEqualTo('filter_route') ->object($filter->setMethod(null)) - ->isInstanceOf('M6Web\Bundle\LogBridgeBundle\Config\Filter') + ->isInstanceOf(\M6Web\Bundle\LogBridgeBundle\Config\Filter::class) ->variable($filter->getMethod()) ->isEqualTo(null) ->object($filter->setStatus([200, 301])) - ->isInstanceOf('M6Web\Bundle\LogBridgeBundle\Config\Filter') + ->isInstanceOf(\M6Web\Bundle\LogBridgeBundle\Config\Filter::class) ->array($filter->getStatus()) ->isIdenticalTo([200, 301]) ; diff --git a/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Config/FilterCollection.php b/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Config/FilterCollection.php index 0882acf..094775a 100644 --- a/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Config/FilterCollection.php +++ b/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Config/FilterCollection.php @@ -7,7 +7,7 @@ class FilterCollection extends atoum { - private function createFilter($name, $route, $method, $status) + private function createFilter(string $name, string $route, ?array $method, ?array $status): Config\Filter { $filter = new Config\Filter($name); $filter @@ -18,11 +18,11 @@ private function createFilter($name, $route, $method, $status) return $filter; } - public function testCollection() + public function testCollection(): void { $filters = []; - $filters[] = $this->createFilter('filter_un', 'get_clip', 'all', 'all'); - $filters[] = $this->createFilter('filter_deux', 'put_clip', ['PUT'], 'all'); + $filters[] = $this->createFilter('filter_un', 'get_clip', ['all'], ['all']); + $filters[] = $this->createFilter('filter_deux', 'put_clip', ['PUT'], ['all']); $filters[] = $this->createFilter('filter_trois', 'put_clip', ['PUT'], [400, 404, 422, 500]); $filterQuatre =$this->createFilter('filter_quatre', 'post_clip', ['POST'], [400, 404, 422, 500]); @@ -33,19 +33,19 @@ public function testCollection() ->integer($collection->count()) ->isEqualTo(3) ->object($filterUn = $collection->getByName('filter_un')) - ->isInstanceOf('M6Web\Bundle\LogBridgeBundle\Config\Filter') + ->isInstanceOf(\M6Web\Bundle\LogBridgeBundle\Config\Filter::class) ->variable($collection->getByName('filter_invalid')) ->isNull() ->object($filterIt = $collection->get(0)) - ->isInstanceOf('M6Web\Bundle\LogBridgeBundle\Config\Filter') + ->isInstanceOf(\M6Web\Bundle\LogBridgeBundle\Config\Filter::class) ->string($filterUn->getName()) ->isEqualTo($filterIt->getName()) ->array($collection->getKeys()) ->hasSize($collection->count()) ->object($collection->add($filterQuatre)) - ->isInstanceOf('M6Web\Bundle\LogBridgeBundle\Config\FilterCollection') + ->isInstanceOf(\M6Web\Bundle\LogBridgeBundle\Config\FilterCollection::class) ->object($collection->getByName($filterQuatre->getName())) - ->isInstanceOf('M6Web\Bundle\LogBridgeBundle\Config\Filter') + ->isInstanceOf(\M6Web\Bundle\LogBridgeBundle\Config\Filter::class) ->string($collection->getKey(3)) ->isEqualTo($filterQuatre->getName()) ; diff --git a/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Config/FilterParser.php b/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Config/FilterParser.php index 64bce35..3997e6e 100644 --- a/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Config/FilterParser.php +++ b/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Config/FilterParser.php @@ -10,12 +10,12 @@ */ class FilterParser extends BaseTest { - private function getParser() + private function getParser(): Config\FilterParser { return new Config\FilterParser($this->getMockedRouter()); } - private function getConfig() + private function getConfig(): array { return [ 'filter_un' => [ @@ -56,7 +56,7 @@ private function getConfig() ]; } - public function testValidParse() + public function testValidParse(): void { $config = $this->getConfig(); @@ -110,7 +110,7 @@ public function testValidParse() } - public function testInvalidRoute() + public function testInvalidRoute(): void { $config = $this->getConfig()['filter_route_invalid']; @@ -120,13 +120,13 @@ public function testInvalidRoute() ->exception(function() use ($parser, $config) { $parser->parse('filter_route_invalid', $config); }) - ->isInstanceOf('M6Web\Bundle\LogBridgeBundle\Config\ParseException') + ->isInstanceOf(\M6Web\Bundle\LogBridgeBundle\Config\ParseException::class) ->message ->contains('Undefined route') ; } - public function testInvalidMethod() + public function testInvalidMethod(): void { $config = $this->getConfig()['filter_method_invalid']; @@ -136,13 +136,13 @@ public function testInvalidMethod() ->exception(function() use ($parser, $config) { $parser->parse('filter_method_invalid', $config); }) - ->isInstanceOf('M6Web\Bundle\LogBridgeBundle\Config\ParseException') + ->isInstanceOf(\TypeError::class) ->message - ->contains('Unrecognized value') + ->contains('must be of type ?array, string given') ; } - public function testInvalidStatus() + public function testInvalidStatus(): void { $config = $this->getConfig()['filter_status_invalid']; @@ -152,9 +152,9 @@ public function testInvalidStatus() ->exception(function() use ($parser, $config) { $parser->parse('filter_status_invalid', $config); }) - ->isInstanceOf('M6Web\Bundle\LogBridgeBundle\Config\ParseException') + ->isInstanceOf(\TypeError::class) ->message - ->contains('Unrecognized value') + ->contains('must be of type ?array, int given') ; } } diff --git a/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Config/Parser.php b/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Config/Parser.php index 8f04522..9ca0f43 100644 --- a/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Config/Parser.php +++ b/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Config/Parser.php @@ -2,7 +2,7 @@ namespace M6Web\Bundle\LogBridgeBundle\Tests\Units\Config; -use M6Web\Bundle\LogBridgeBundle\Tests\Units\BaseTest; +use M6Web\Bundle\LogBridgeBundle\Tests\Units\BaseTest; use M6Web\Bundle\LogBridgeBundle\Config; /** @@ -15,17 +15,17 @@ private function getRouter() return new MockRouter(); } - private function getParser() + private function getParser(): Config\Parser { return new Config\Parser($this->getMockedRouter()); } - private function getConfig() + private function getConfig(): array { return [ 'active_filters' => [ - 'filter_un', - 'filter_trois' + 'filter_un', + 'filter_trois' ], 'filters' => [ 'filter_un' => [ @@ -53,7 +53,7 @@ private function getConfig() } - public function testValidConfig() + public function testValidConfig(): void { $config = $this->getConfig(); @@ -61,19 +61,19 @@ public function testValidConfig() ->if($parser = $this->getParser()) ->then ->object($configuration = $parser->parse($config)) - ->isInstanceOf('M6Web\Bundle\LogBridgeBundle\Config\Configuration') + ->isInstanceOf(\M6Web\Bundle\LogBridgeBundle\Config\Configuration::class) ->array($configuration->getActiveFilters()) - ->hasSize(count($config['active_filters'])) + ->hasSize(is_countable($config['active_filters']) ? count($config['active_filters']) : 0) ->hasKeys(array_keys($config['active_filters'])) ->object($collection = $configuration->getFilters()) - ->isInstanceOf('M6Web\Bundle\LogBridgeBundle\Config\FilterCollection') + ->isInstanceOf(\M6Web\Bundle\LogBridgeBundle\Config\FilterCollection::class) ->integer($collection->count()) - ->isEqualTo(count($config['filters'])) + ->isEqualTo(is_countable($config['filters']) ? count($config['filters']) : 0) ; } - public function testInvalidConfig() + public function testInvalidConfig(): void { $config = [ 'filters' => [ @@ -101,7 +101,7 @@ public function testInvalidConfig() ->exception(function() use($parser, $config) { $parser->parse($config); }) - ->isInstanceOf('M6Web\Bundle\LogBridgeBundle\Config\ParseException') + ->isInstanceOf(\M6Web\Bundle\LogBridgeBundle\Config\ParseException::class) ; } diff --git a/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/EventDispatcher/BuilderEvent.php b/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/EventDispatcher/BuilderEvent.php index 7f204d0..1521efb 100644 --- a/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/EventDispatcher/BuilderEvent.php +++ b/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/EventDispatcher/BuilderEvent.php @@ -9,7 +9,7 @@ class BuilderEvent extends atoum { - public function testBuildEventIsInstanciable() + public function testBuildEventIsInstanciable(): void { $this ->given($this->mockGenerator->orphanize('__construct')) diff --git a/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/EventDispatcher/LogExceptionListener.php b/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/EventDispatcher/LogExceptionListener.php index d430980..2a28785 100644 --- a/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/EventDispatcher/LogExceptionListener.php +++ b/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/EventDispatcher/LogExceptionListener.php @@ -10,7 +10,7 @@ class LogExceptionListener { - public function testOnKernelExceptionCall() + public function testOnKernelExceptionCall(): void { $this ->given( diff --git a/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Formatter/DefaultFormatter.php b/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Formatter/DefaultFormatter.php index 4b9f9bf..9f1a6b1 100644 --- a/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Formatter/DefaultFormatter.php +++ b/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Formatter/DefaultFormatter.php @@ -8,17 +8,18 @@ use M6Web\Bundle\LogBridgeBundle\Formatter; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\Security\Core\User\UserInterface; /** * DefaultFormatter */ class DefaultFormatter extends atoum { - const ENVIRONMENT = 'test'; - const USERNAME = 'test-username'; - const PASSWORD = 'test-password'; + private const ENVIRONMENT = 'test'; + private const USERNAME = 'test-username'; + private const PASSWORD = 'test-password'; - private function getUser() + private function getUser(): UserInterface { if (class_exists(User::class)) { return new User(self::USERNAME, self::PASSWORD); @@ -30,7 +31,7 @@ private function getUser() private function getMockedToken() { $usernameMethod = 'getUserIdentifier'; - if (method_exists('Symfony\Component\Security\Core\Authentication\Token\TokenInterface', 'getUsername')) { + if (method_exists(\Symfony\Component\Security\Core\Authentication\Token\TokenInterface::class, 'getUsername')) { // compatibility Symfony < 6 $usernameMethod = 'getUsername'; } @@ -53,14 +54,12 @@ private function getMockedTokenStorage() return $context; } - private function createProvider($environment = self::ENVIRONMENT, array $ignores = array('php-auth-pw'), $prefix = '') + private function createProvider(string $environment = self::ENVIRONMENT, array $ignores = array('php-auth-pw'), string $prefix = ''): Formatter\DefaultFormatter { - $provider = new Formatter\DefaultFormatter($environment, $ignores, $prefix); - - return $provider; + return new Formatter\DefaultFormatter($environment, $ignores, $prefix); } - public function testProvider() + public function testProvider(): void { $request = new Request(); $response = new Response('Body content response'); @@ -73,7 +72,7 @@ public function testProvider() ->if($provider = $this->createProvider()) ->then ->object($provider->setTokenStorage($tokenstorage)) - ->isInstanceOf('M6Web\Bundle\LogBridgeBundle\Formatter\DefaultFormatter') + ->isInstanceOf(\M6Web\Bundle\LogBridgeBundle\Formatter\DefaultFormatter::class) ->string($provider->getLogContent($request, $response, [])) ->contains('HTTP 1.0 200') ->contains('Cache-Control') @@ -102,7 +101,7 @@ public function testProvider() } - public function testPostProvider() + public function testPostProvider(): void { $post = [ 'postVar1' => 'value un', @@ -123,7 +122,7 @@ public function testPostProvider() ->if($provider = $this->createProvider()) ->then ->object($provider->setTokenStorage($tokenstorage)) - ->isInstanceOf('M6Web\Bundle\LogBridgeBundle\Formatter\DefaultFormatter') + ->isInstanceOf(\M6Web\Bundle\LogBridgeBundle\Formatter\DefaultFormatter::class) ->string($provider->getLogContent($request, $response, ['post_parameters' => true])) ->contains('HTTP 1.0 200') ->contains('Cache-Control') diff --git a/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Formatter/ExceptionFormatter.php b/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Formatter/ExceptionFormatter.php index 3a548c9..2547cdf 100644 --- a/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Formatter/ExceptionFormatter.php +++ b/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Formatter/ExceptionFormatter.php @@ -8,30 +8,25 @@ use M6Web\Bundle\LogBridgeBundle\Formatter; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\Security\Core\User\UserInterface; /** * ExceptionFormatter */ class ExceptionFormatter extends atoum { - const ENVIRONMENT = 'test'; - const USERNAME = 'test-username'; - const PASSWORD = 'test-password'; - - /** - * @var string - */ - private $requestExceptionAttribute; - - /** - * @param string $method - */ - public function beforeTestMethod($method) + private const ENVIRONMENT = 'test'; + private const USERNAME = 'test-username'; + private const PASSWORD = 'test-password'; + + private ?string $requestExceptionAttribute = null; + + public function beforeTestMethod(string $method): void { $this->requestExceptionAttribute = 'LogBridgeBundle_'.time(); } - private function getUser() + private function getUser(): UserInterface { if (class_exists(User::class)) { return new User(self::USERNAME, self::PASSWORD); @@ -43,7 +38,7 @@ private function getUser() private function getMockedToken() { $usernameMethod = 'getUserIdentifier'; - if (method_exists('Symfony\Component\Security\Core\Authentication\Token\TokenInterface', 'getUsername')) { + if (method_exists(\Symfony\Component\Security\Core\Authentication\Token\TokenInterface::class, 'getUsername')) { // compatibility Symfony < 6 $usernameMethod = 'getUsername'; } @@ -66,7 +61,7 @@ private function getMockedTokenStorage() return $context; } - private function createProvider($environment = self::ENVIRONMENT, array $ignores = ['php-auth-pw'], $prefix = '') + private function createProvider(string $environment = self::ENVIRONMENT, array $ignores = ['php-auth-pw'], string $prefix = ''): Formatter\ExceptionFormatter { $provider = new Formatter\ExceptionFormatter($environment, $ignores, $prefix); $provider->setRequestExceptionAttribute($this->requestExceptionAttribute); @@ -74,7 +69,7 @@ private function createProvider($environment = self::ENVIRONMENT, array $ignores return $provider; } - public function testProvider() + public function testProvider(): void { $request = new Request(); $response = new Response('Body content response'); @@ -91,7 +86,7 @@ public function testProvider() ->if($provider = $this->createProvider()) ->then ->object($provider->setTokenStorage($tokenstorage)) - ->isInstanceOf('M6Web\Bundle\LogBridgeBundle\Formatter\ExceptionFormatter') + ->isInstanceOf(\M6Web\Bundle\LogBridgeBundle\Formatter\ExceptionFormatter::class) ->string($provider->getLogContent($request, $response, [])) ->contains('HTTP 1.0 200') ->contains('Cache-Control') @@ -127,14 +122,14 @@ public function testProvider() ; } - public function testProviderExceptionsDepth() + public function testProviderExceptionsDepth(): void { $request = new Request(); $response = new Response('Body content response'); $tokenstorage = $this->getMockedTokenStorage(); $exception3 = new \Exception('Test: first exception thrown.'); - $exception2 = new \Exception('Test: second exception thrown.', null, $exception3); - $exception1 = new \Exception('Test: third exception thrown.', null, $exception2); + $exception2 = new \Exception('Test: second exception thrown.', 0, $exception3); + $exception1 = new \Exception('Test: third exception thrown.', 0, $exception2); $this ->given( @@ -143,7 +138,7 @@ public function testProviderExceptionsDepth() ->if($provider = $this->createProvider()) ->then ->object($provider->setTokenStorage($tokenstorage)) - ->isInstanceOf('M6Web\Bundle\LogBridgeBundle\Formatter\ExceptionFormatter') + ->isInstanceOf(\M6Web\Bundle\LogBridgeBundle\Formatter\ExceptionFormatter::class) ->string($provider->getLogContent($request, $response, [])) ->contains('Exception message (1) :') ->contains($exception1->getMessage()) @@ -160,7 +155,7 @@ public function testProviderExceptionsDepth() ; } - public function testTypeErrorException() + public function testTypeErrorException(): void { $request = new Request(); $exception = new \TypeError('Test: TypeError exception thrown.'); @@ -172,7 +167,7 @@ public function testTypeErrorException() ->if($provider = $this->createProvider()) ->then ->object($provider->setTokenStorage($this->getMockedTokenStorage())) - ->isInstanceOf('M6Web\Bundle\LogBridgeBundle\Formatter\ExceptionFormatter') + ->isInstanceOf(\M6Web\Bundle\LogBridgeBundle\Formatter\ExceptionFormatter::class) ->string($provider->getLogContent($request, new Response('Body content response'), [])) ->contains('Exception message (1) :') ->contains($exception->getMessage()) diff --git a/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Matcher/BaseMatcher.php b/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Matcher/BaseMatcher.php index 9135093..d3e03f6 100644 --- a/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Matcher/BaseMatcher.php +++ b/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Matcher/BaseMatcher.php @@ -7,7 +7,7 @@ class BaseMatcher extends BaseTest { - protected function cacheClear() + protected function cacheClear(): void { $files = glob($this->getCacheDir() .'/*'); @@ -18,23 +18,23 @@ protected function cacheClear() } } - protected function getCacheDir() + protected function getCacheDir(): string { return __DIR__.'/../../Fixtures/Resources/cache'; } - protected function getParser() + protected function getParser(): Parser { return new Parser($this->getMockedRouter()); } - protected function getMatcherClassName() + protected function getMatcherClassName(): string { return 'TestLogMatcher'; } - protected function getActiveFilters() + protected function getActiveFilters(): array { return [ 'get_clip_all_status', @@ -43,7 +43,7 @@ protected function getActiveFilters() ]; } - protected function getFilters() + protected function getFilters(): array { return [ 'get_clip_all_status' => [ diff --git a/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Matcher/Builder.php b/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Matcher/Builder.php index b62c9d1..a5c5289 100644 --- a/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Matcher/Builder.php +++ b/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Matcher/Builder.php @@ -4,11 +4,10 @@ use atoum; use M6Web\Bundle\LogBridgeBundle\Matcher; -use M6Web\Bundle\LogBridgeBundle\Config\Parser; class Builder extends BaseMatcher { - private function getTypeManager() + private function getTypeManager(): Matcher\Status\TypeManager { $typeManager = new Matcher\Status\TypeManager(); @@ -20,17 +19,7 @@ private function getTypeManager() return $typeManager; } - public function getParserMock() - { - $this->mockGenerator->orphanize('__construct'); - $this->mockGenerator->shuntParentClassCalls(); - - $parserMock = new \mock\M6Web\Bundle\LogBridgeBundle\Config\Parser(); - - return $parserMock; - } - - public function testBuilder() + public function testBuilder(): void { $this ->given( @@ -39,10 +28,10 @@ public function testBuilder() $matcherClass = $this->getMatcherClassName(), $typeManager = $this->getTypeManager() ) - ->if($builder = new Matcher\Builder($typeManager, $this->getFilters(), $this->getActiveFilters(), 'test')) + ->if($builder = new Matcher\Builder($typeManager, $this->getFilters(), $this->getActiveFilters())) ->then ->object($builder->setCacheDir($cacheDir)) - ->isInstanceOf('M6Web\Bundle\LogBridgeBundle\Matcher\Builder') + ->isInstanceOf(\M6Web\Bundle\LogBridgeBundle\Matcher\Builder::class) ->string($builder->getCacheDir()) ->isEqualTo($cacheDir) ->boolean($builder->isDebug(false)) @@ -52,9 +41,9 @@ public function testBuilder() ->boolean($builder->isDebug()) ->isTrue() ->object($builder->setConfigParser($this->getParser())) - ->isInstanceOf('M6Web\Bundle\LogBridgeBundle\Matcher\Builder') + ->isInstanceOf(\M6Web\Bundle\LogBridgeBundle\Matcher\Builder::class) ->object($builder->setMatcherClassName($matcherClass)) - ->isInstanceOf('M6Web\Bundle\LogBridgeBundle\Matcher\Builder') + ->isInstanceOf(\M6Web\Bundle\LogBridgeBundle\Matcher\Builder::class) ->string($builder->getMatcherClassName()) ->isEqualTo($matcherClass) ->string($builder->getAbsoluteCachePath()) diff --git a/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Matcher/Dumper/PhpMatcherDumper.php b/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Matcher/Dumper/PhpMatcherDumper.php index 79f7979..8da6b61 100644 --- a/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Matcher/Dumper/PhpMatcherDumper.php +++ b/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Matcher/Dumper/PhpMatcherDumper.php @@ -22,7 +22,7 @@ private function getTypeManager() private function getBuilder() { - $builder = new Matcher\Builder($this->getTypeManager(), $this->getFilters(), $this->getActiveFilters(), 'test'); + $builder = new Matcher\Builder($this->getTypeManager(), $this->getFilters(), $this->getActiveFilters()); $builder ->setMatcherClassName($this->getMatcherClassName()) diff --git a/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Matcher/MatcherProxy.php b/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Matcher/MatcherProxy.php index 12e8d79..8507062 100644 --- a/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Matcher/MatcherProxy.php +++ b/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Matcher/MatcherProxy.php @@ -6,7 +6,7 @@ class MatcherProxy extends BaseMatcher { - private function getTypeManager() + private function getTypeManager(): Matcher\Status\TypeManager { $typeManager = new Matcher\Status\TypeManager(); @@ -18,9 +18,9 @@ private function getTypeManager() return $typeManager; } - private function getBuilder() + private function getBuilder(): Matcher\Builder { - $builder = new Matcher\Builder($this->getTypeManager(), $this->getFilters(), $this->getActiveFilters(), 'test'); + $builder = new Matcher\Builder($this->getTypeManager(), $this->getFilters(), $this->getActiveFilters()); $builder ->setMatcherClassName($this->getMatcherClassName()) @@ -30,7 +30,7 @@ private function getBuilder() return $builder; } - private function createFilters($matcher) + private function createFilters($matcher): array { $keyNoConfig = $matcher->generateFilterKey('dynamically_no_config', 'GET', 200); $key500 = $matcher->generateFilterKey('dynamically_un', 'POST', 500); @@ -49,13 +49,13 @@ private function createFilters($matcher) ]; } - public function testProxy() + public function testProxy(): void { $this ->if($proxy = new Matcher\MatcherProxy($this->getBuilder())) ->then ->object($proxy->getBuilder()) - ->isInstanceOf('M6Web\Bundle\LogBridgeBundle\Matcher\Builder') + ->isInstanceOf(\M6Web\Bundle\LogBridgeBundle\Matcher\Builder::class) ->object($proxy->getMatcher()) ->isInstanceOf($this->getMatcherClassName()) ->boolean($proxy->match('get_clip', 'GET', 200)) @@ -67,23 +67,23 @@ public function testProxy() ; } - public function testDynamicallyFilter() + public function testDynamicallyFilter(): void { $this ->if($proxy = new Matcher\MatcherProxy($this->getBuilder())) ->then ->object($proxy->getBuilder()) - ->isInstanceOf('M6Web\Bundle\LogBridgeBundle\Matcher\Builder') + ->isInstanceOf(\M6Web\Bundle\LogBridgeBundle\Matcher\Builder::class) ->object($matcher = $proxy->getMatcher()) ->isInstanceOf($this->getMatcherClassName()) ->boolean($proxy->match('get_program_dynamically', 'GET', 200)) ->isFalse() ->object($proxy->addFilter($matcher->generateFilterKey('get_program_dynamically', 'GET', 200))) - ->isInstanceOf('M6Web\Bundle\LogBridgeBundle\Matcher\MatcherProxy') + ->isInstanceOf(\M6Web\Bundle\LogBridgeBundle\Matcher\MatcherProxy::class) ->boolean($proxy->match('get_program_dynamically', 'GET', 200)) ->isTrue() ->object($proxy->setFilters($this->createFilters($matcher))) - ->isInstanceOf('M6Web\Bundle\LogBridgeBundle\Matcher\MatcherProxy') + ->isInstanceOf(\M6Web\Bundle\LogBridgeBundle\Matcher\MatcherProxy::class) ->boolean($proxy->match('dynamically_un', 'POST', 500)) ->isTrue() ->boolean($proxy->match('dynamically_deux', 'PUT', 422)) diff --git a/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Matcher/Status/Type/AbstractType.php b/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Matcher/Status/Type/AbstractType.php index e7f5680..77b9422 100644 --- a/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Matcher/Status/Type/AbstractType.php +++ b/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Matcher/Status/Type/AbstractType.php @@ -17,29 +17,29 @@ public function getSample1TypeMock() return $sample1Type; } - public function testMatch() + public function testMatch(): void { $this ->given( $type = new TestedClass() ) ->then - ->integer($type->match('350')) - ->isEqualTo(1) - ->integer($type->match('3500')) - ->isEqualTo(0) - ->integer($type->match('50')) - ->isEqualTo(0) - ->integer($type->match('!350')) - ->isEqualTo(1) - ->integer($type->match('!3500')) - ->isEqualTo(0) - ->integer($type->match('!50')) - ->isEqualTo(0) + ->boolean($type->match('350')) + ->isTrue() + ->boolean($type->match('3500')) + ->isFalse() + ->boolean($type->match('50')) + ->isFalse() + ->boolean($type->match('!350')) + ->isTrue() + ->boolean($type->match('!3500')) + ->isFalse() + ->boolean($type->match('!50')) + ->isFalse() ; } - public function testGetStatus() + public function testGetStatus(): void { $this ->given( @@ -53,15 +53,15 @@ public function testGetStatus() ->isEqualTo(['200']) ->exception( function () use ($typeBadTransform) { - $status = $typeBadTransform->getStatus('200'); + $typeBadTransform->getStatus('200'); } ) - ->hasCode(500) - ->hasMessage('"transform" method must be return an array in class "mock\M6Web\Bundle\LogBridgeBundle\Tests\Fixtures\Matcher\Status\Type\Sample1Type"') + ->hasCode(0) + ->hasMessage('mock\M6Web\Bundle\LogBridgeBundle\Tests\Fixtures\Matcher\Status\Type\Sample1Type::transform(): Return value must be of type array, string returned') ; } - public function testIsExclude() + public function testIsExclude(): void { $this ->given( diff --git a/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Matcher/Status/Type/CompleteType.php b/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Matcher/Status/Type/CompleteType.php index 7917efa..00afbbb 100644 --- a/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Matcher/Status/Type/CompleteType.php +++ b/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Matcher/Status/Type/CompleteType.php @@ -11,11 +11,8 @@ class CompleteType extends atoum * Test get status * * @dataProvider dataTestGetStatus - * - * @param string $config - * @param array $statusResult */ - public function testGetStatus($config, $statusResult) + public function testGetStatus(string $config, array $statusResult): void { $this ->given( @@ -30,7 +27,7 @@ public function testGetStatus($config, $statusResult) /** * Data to testGetStatus */ - public function dataTestGetStatus() + public function dataTestGetStatus(): array { return [ [ diff --git a/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Matcher/Status/Type/RangeType.php b/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Matcher/Status/Type/RangeType.php index 5658849..d681f60 100644 --- a/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Matcher/Status/Type/RangeType.php +++ b/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Matcher/Status/Type/RangeType.php @@ -11,11 +11,8 @@ class RangeType extends atoum * Test get status * * @dataProvider dataTestGetStatus - * - * @param string $config - * @param array $statusResult */ - public function testGetStatus($config, $statusResult) + public function testGetStatus(string $config, array $statusResult): void { $this ->given( @@ -30,7 +27,7 @@ public function testGetStatus($config, $statusResult) /** * Data to testGetStatus */ - public function dataTestGetStatus() + public function dataTestGetStatus(): array { return [ [ diff --git a/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Matcher/Status/Type/SimpleType.php b/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Matcher/Status/Type/SimpleType.php index 63dc704..f2b7cac 100644 --- a/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Matcher/Status/Type/SimpleType.php +++ b/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Matcher/Status/Type/SimpleType.php @@ -11,11 +11,8 @@ class SimpleType extends atoum * Test get status * * @dataProvider dataTestGetStatus - * - * @param string $config - * @param array $statusResult */ - public function testGetStatus($config, $statusResult) + public function testGetStatus(string $config, array $statusResult): void { $this ->given( @@ -30,7 +27,7 @@ public function testGetStatus($config, $statusResult) /** * Data to testGetStatus */ - public function dataTestGetStatus() + public function dataTestGetStatus(): array { return [ [ diff --git a/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Matcher/Status/Type/UpperType.php b/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Matcher/Status/Type/UpperType.php index 082d0c6..ba312e6 100644 --- a/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Matcher/Status/Type/UpperType.php +++ b/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Matcher/Status/Type/UpperType.php @@ -11,11 +11,8 @@ class UpperType extends atoum * Test get status * * @dataProvider dataTestGetStatus - * - * @param string $config - * @param array $statusResult */ - public function testGetStatus($config, $statusResult) + public function testGetStatus(string $config, array $statusResult) { $this ->given( @@ -30,7 +27,7 @@ public function testGetStatus($config, $statusResult) /** * Data to testGetStatus */ - public function dataTestGetStatus() + public function dataTestGetStatus(): array { return [ [ diff --git a/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Matcher/Status/TypeManager.php b/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Matcher/Status/TypeManager.php index 1347a97..331e029 100644 --- a/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Matcher/Status/TypeManager.php +++ b/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Matcher/Status/TypeManager.php @@ -3,33 +3,23 @@ namespace M6Web\Bundle\LogBridgeBundle\Tests\Units\Matcher\Status; use atoum; +use M6Web\Bundle\LogBridgeBundle\Matcher\Status\Type\AbstractType; use M6Web\Bundle\LogBridgeBundle\Matcher\Status\Type\TypeInterface; use M6Web\Bundle\LogBridgeBundle\Matcher\Status\TypeManager as TestedClass; class TypeManager extends atoum { - /** - * Get type - * - * @param integer $number - * - * @return M6Web\Bundle\LogBridgeBundle\Tests\Fixtures\Matcher\Status\Type\Sample1Type - */ - private function getType($number){ + private function getType($number): AbstractType + { $classType = sprintf('M6Web\Bundle\LogBridgeBundle\Tests\Fixtures\Matcher\Status\Type\Sample%dType', $number); - $type = new $classType(); - - return $type; + return new $classType(); } /** * @dataProvider dataTestAddType - * - * @param array $types - * @param array $result */ - public function testAddType(array $types, array $result) { - + public function testAddType(array $types, array $result): void + { $this ->given( $typeManager = new TestedClass() @@ -48,11 +38,9 @@ public function testAddType(array $types, array $result) { /** * Data to testAddType - * - * @return array */ - public function dataTestAddType() { - + public function dataTestAddType(): array + { $type1 = $this->getType(1); $type2 = $this->getType(2); $type3 = $this->getType(3); @@ -65,24 +53,19 @@ public function dataTestAddType() { $type2, ], [ - get_class($type1) => $type1, - get_class($type3) => $type3, - get_class($type2) => $type2, + $type1::class => $type1, + $type3::class => $type3, + $type2::class => $type2, ] ] ]; - } /** * @dataProvider dataTestRemoveType - * - * @param array $types - * @param TypeInterface $typeToRemove - * @param array $result */ - public function testRemoveType(array $types, $typeToRemove, array $result) { - + public function testRemoveType(array $types, TypeInterface $typeToRemove, array $result): void + { $this ->given( $typeManager = new TestedClass() @@ -102,11 +85,9 @@ public function testRemoveType(array $types, $typeToRemove, array $result) { /** * Data to testRemoveType - * - * @return array */ - public function dataTestRemoveType() { - + public function dataTestRemoveType(): array + { $type1 = $this->getType(1); $type2 = $this->getType(2); $type3 = $this->getType(3); @@ -120,11 +101,10 @@ public function dataTestRemoveType() { ], $type2, [ - get_class($type3) => $type3, - get_class($type1) => $type1, + $type3::class => $type3, + $type1::class => $type1, ] ] ]; - } }