From e3f5fbd38fa0873b4834581bda31d2924ac57f3b Mon Sep 17 00:00:00 2001 From: Igor Kusmitsch Date: Wed, 25 Sep 2024 16:05:53 -0300 Subject: [PATCH 01/15] Update project dependencies - Require PHP 8.2 or higher - Require PHPUnit 11 - Require pcov extension for code coverage - Add squizlabs/php_codesniffer for code standards - Require symfony/cache for caching - Remove Doctrine Cache and Annotations - Remove laminas/laminas-zendframework-bridge --- composer.json | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/composer.json b/composer.json index 27d3e12..3a34904 100644 --- a/composer.json +++ b/composer.json @@ -14,16 +14,15 @@ ], "require": { - "php": "~8.0 || ~8.1", - "doctrine/annotations": "^1.13", - "laminas/laminas-zendframework-bridge": "^1.0" + "php": "^8.2" }, "require-dev": { - "doctrine/cache": "~1.3", - "phpunit/phpunit": "^9", - "laminas/laminas-filter": "^2.9", - "dms/coding-standard": "^12" + "ext-pcov": "*", + "phpunit/phpunit": "^11", + "laminas/laminas-filter": "^2.37", + "squizlabs/php_codesniffer": "3.*", + "symfony/cache": "^6.4 | ^7.0" }, "autoload": { "psr-0": { From 9f05498368e739a11cea8c56568ad9afecb11800 Mon Sep 17 00:00:00 2001 From: Igor Kusmitsch Date: Wed, 25 Sep 2024 16:11:39 -0300 Subject: [PATCH 02/15] Update PHPUnit configuration --- phpunit.xml | 59 +++++++++++++++++++++++++++++++++++------------------ 1 file changed, 39 insertions(+), 20 deletions(-) diff --git a/phpunit.xml b/phpunit.xml index a4ef660..ced792b 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,21 +1,40 @@ - - - - - src/DMS - - - - - - - - tests/DMS - - - - - - - + + + + tests + + + + + + src + + + + + + + + + + + + \ No newline at end of file From 5508e4b62bc30db7908b55e4354de1987e6dd75f Mon Sep 17 00:00:00 2001 From: Igor Kusmitsch Date: Wed, 25 Sep 2024 16:13:17 -0300 Subject: [PATCH 03/15] Update PHPCS rules --- phpcs.xml.dist | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/phpcs.xml.dist b/phpcs.xml.dist index c3a78fe..06d9a96 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -1,20 +1,9 @@ - - - - - - - + src + tests - - - - - - - - + vendor/* + \ No newline at end of file From dd7c4ac1db6d0b16267580e32fe44e66eca53f6a Mon Sep 17 00:00:00 2001 From: Igor Kusmitsch Date: Wed, 25 Sep 2024 16:15:41 -0300 Subject: [PATCH 04/15] Delete project files marked as deprecated previously --- src/DMS/Filter/Filters/Zend.php | 67 --------------------------- src/DMS/Filter/Rules/Zend.php | 37 --------------- tests/DMS/Filter/Filters/ZendTest.php | 46 ------------------ 3 files changed, 150 deletions(-) delete mode 100644 src/DMS/Filter/Filters/Zend.php delete mode 100644 src/DMS/Filter/Rules/Zend.php delete mode 100644 tests/DMS/Filter/Filters/ZendTest.php diff --git a/src/DMS/Filter/Filters/Zend.php b/src/DMS/Filter/Filters/Zend.php deleted file mode 100644 index 0f4b947..0000000 --- a/src/DMS/Filter/Filters/Zend.php +++ /dev/null @@ -1,67 +0,0 @@ -getZendInstance($rule->class, $rule->zendOptions)->filter($value); - } - - /** - * Instantiates a configured Zend Filter, if it exists - * - * @param mixed[] $options - * - * @return FilterInterface|object - * - * @throws InvalidZendFilterException - */ - public function getZendInstance(string $class, array $options): object - { - if (strpos($class, 'Zend\Filter') === false) { - $class = 'Zend\Filter\\' . $class; - } - - if (! class_exists($class)) { - throw new InvalidZendFilterException(sprintf('Could not find or autoload: %s', $class)); - } - - try { - new ReflectionMethod($class, 'setOptions'); - - $filter = new $class(); - $filter->setOptions($options); - - return $filter; - } catch (ReflectionException) { - return new $class($options); - } - } -} diff --git a/src/DMS/Filter/Rules/Zend.php b/src/DMS/Filter/Rules/Zend.php deleted file mode 100644 index 9fdde18..0000000 --- a/src/DMS/Filter/Rules/Zend.php +++ /dev/null @@ -1,37 +0,0 @@ -buildRule('Boolean', ['casting' => false]); - $filter = new Zend(); - $filter->apply($rule, '0'); - $this->expectNotToPerformAssertions(); - } - - public function testFilterFullname(): void - { - $rule = $this->buildRule('Zend\Filter\Boolean', ['casting' => false]); - $filter = new Zend(); - $filter->apply($rule, '0'); - $this->expectNotToPerformAssertions(); - } - - public function testInvalidFilter(): void - { - $this->expectException(InvalidZendFilterException::class); - $rule = $this->buildRule('MissingFilter'); - $filter = new Zend(); - $filter->apply($rule, '0'); - } - - protected function buildRule($class, $options = []): ZendRule - { - return new ZendRule( - [ - 'class' => $class, - 'zendOptions' => $options, - ] - ); - } -} From 7c2b062136a3d9aab7b26097055a672b32416422 Mon Sep 17 00:00:00 2001 From: Igor Kusmitsch Date: Wed, 25 Sep 2024 16:17:52 -0300 Subject: [PATCH 05/15] Delete project files related to Doctrine Annotations --- .../Mapping/Loader/AnnotationLoader.php | 69 ------------------ .../Tests/Dummy/Classes/AnnotatedClass.php | 71 ------------------- .../Dummy/Classes/ChildAnnotatedClass.php | 15 ---- 3 files changed, 155 deletions(-) delete mode 100644 src/DMS/Filter/Mapping/Loader/AnnotationLoader.php delete mode 100644 tests/DMS/Tests/Dummy/Classes/AnnotatedClass.php delete mode 100644 tests/DMS/Tests/Dummy/Classes/ChildAnnotatedClass.php diff --git a/src/DMS/Filter/Mapping/Loader/AnnotationLoader.php b/src/DMS/Filter/Mapping/Loader/AnnotationLoader.php deleted file mode 100644 index b594612..0000000 --- a/src/DMS/Filter/Mapping/Loader/AnnotationLoader.php +++ /dev/null @@ -1,69 +0,0 @@ -getReflectionClass(); - - //Iterate over properties to get annotations - foreach ($reflClass->getProperties() as $property) { - $this->readProperty($property, $metadata); - } - - return true; - } - - /** - * Reads annotations for a selected property in the class - */ - private function readProperty(ReflectionProperty $property, ClassMetadataInterface $metadata): void - { - // Skip if this property is not from this class - if ( - $property->getDeclaringClass()->getName() - !== $metadata->getClassName() - ) { - return; - } - - //Iterate over all annotations - foreach ($this->reader->getPropertyAnnotations($property) as $rule) { - //Skip is its not a rule - if (! $rule instanceof Rules\Rule) { - continue; - } - - //Add Rule - $metadata->addPropertyRule($property->getName(), $rule); - } - } -} diff --git a/tests/DMS/Tests/Dummy/Classes/AnnotatedClass.php b/tests/DMS/Tests/Dummy/Classes/AnnotatedClass.php deleted file mode 100644 index fc23190..0000000 --- a/tests/DMS/Tests/Dummy/Classes/AnnotatedClass.php +++ /dev/null @@ -1,71 +0,0 @@ -") - * - * @var string - */ - public string $description; - - /** - * @var string - * @Filter\Callback("callbackMethod") - */ - public ?string $callback = null; - - /** - * @var string - * @Filter\Callback({"DMS\Tests\Dummy\Classes\AnnotatedClass", "anotherCallback"}) - */ - public ?string $callback2 = null; - - /** - * @var string - * @Filter\Zend("StringToLower") - */ - public ?string $zend = null; - - /** - * @var string - * @Filter\Zend(class="Boolean", zendOptions={"casting"=false}) - */ - public ?string $zendalternate = null; - - /** - * @param $value - * @return string - */ - public function callbackMethod($value): string - { - return 'called_back'; - } - - /** - * @param $value - * @return string - */ - public static function anotherCallback($value): string - { - return 'called_back'; - } -} diff --git a/tests/DMS/Tests/Dummy/Classes/ChildAnnotatedClass.php b/tests/DMS/Tests/Dummy/Classes/ChildAnnotatedClass.php deleted file mode 100644 index 4f5ba4f..0000000 --- a/tests/DMS/Tests/Dummy/Classes/ChildAnnotatedClass.php +++ /dev/null @@ -1,15 +0,0 @@ - Date: Wed, 25 Sep 2024 16:20:07 -0300 Subject: [PATCH 06/15] Enable filter rules for using PHP attributes --- src/DMS/Filter/Rules/Alnum.php | 12 +- src/DMS/Filter/Rules/Alpha.php | 12 +- src/DMS/Filter/Rules/BooleanScalar.php | 4 +- src/DMS/Filter/Rules/Callback.php | 14 +- src/DMS/Filter/Rules/Digits.php | 12 +- src/DMS/Filter/Rules/FloatScalar.php | 4 +- src/DMS/Filter/Rules/HtmlEntities.php | 25 ++-- src/DMS/Filter/Rules/IntScalar.php | 4 +- src/DMS/Filter/Rules/Laminas.php | 23 +--- src/DMS/Filter/Rules/PregReplace.php | 21 +-- src/DMS/Filter/Rules/RegExp.php | 4 +- src/DMS/Filter/Rules/Rule.php | 177 ------------------------- src/DMS/Filter/Rules/StripNewlines.php | 4 +- src/DMS/Filter/Rules/StripTags.php | 12 +- src/DMS/Filter/Rules/ToLower.php | 12 +- src/DMS/Filter/Rules/ToUpper.php | 12 +- src/DMS/Filter/Rules/Trim.php | 12 +- 17 files changed, 61 insertions(+), 303 deletions(-) diff --git a/src/DMS/Filter/Rules/Alnum.php b/src/DMS/Filter/Rules/Alnum.php index 7c0cb43..2c3b55e 100644 --- a/src/DMS/Filter/Rules/Alnum.php +++ b/src/DMS/Filter/Rules/Alnum.php @@ -8,19 +8,15 @@ /** * Alnum Rule (Alphanumeric) - * - * @Annotation */ -#[Attribute] +#[Attribute(Attribute::TARGET_PROPERTY)] class Alnum extends RegExp { /** * Allow Whitespace or not */ - public bool $allowWhitespace = true; - - public function getDefaultOption(): string|null - { - return 'allowWhitespace'; + public function __construct( + public bool $allowWhitespace = true + ) { } } diff --git a/src/DMS/Filter/Rules/Alpha.php b/src/DMS/Filter/Rules/Alpha.php index 7c59e58..6e79116 100644 --- a/src/DMS/Filter/Rules/Alpha.php +++ b/src/DMS/Filter/Rules/Alpha.php @@ -8,19 +8,15 @@ /** * Alpha Rule - * - * @Annotation */ -#[Attribute] +#[Attribute(Attribute::TARGET_PROPERTY)] class Alpha extends RegExp { /** * Allow Whitespace or not */ - public bool $allowWhitespace = true; - - public function getDefaultOption(): string|null - { - return 'allowWhitespace'; + public function __construct( + public bool $allowWhitespace = true + ) { } } diff --git a/src/DMS/Filter/Rules/BooleanScalar.php b/src/DMS/Filter/Rules/BooleanScalar.php index 9e03770..d99f932 100644 --- a/src/DMS/Filter/Rules/BooleanScalar.php +++ b/src/DMS/Filter/Rules/BooleanScalar.php @@ -8,10 +8,8 @@ /** * BooleanScalar Rule - * - * @Annotation */ -#[Attribute] +#[Attribute(Attribute::TARGET_PROPERTY)] class BooleanScalar extends Rule { } diff --git a/src/DMS/Filter/Rules/Callback.php b/src/DMS/Filter/Rules/Callback.php index b1391d5..17fc019 100644 --- a/src/DMS/Filter/Rules/Callback.php +++ b/src/DMS/Filter/Rules/Callback.php @@ -13,10 +13,8 @@ /** * Callback Rule - * - * @Annotation */ -#[Attribute] +#[Attribute(Attribute::TARGET_PROPERTY)] class Callback extends Rule { public const SELF_METHOD_TYPE = 'self_method'; @@ -28,14 +26,10 @@ class Callback extends Rule * - string: method of filtered object or function * - array: [Class, Method] to be called * - Closure - * - * @var string|string[]|callable */ - public $callback; - - public function getDefaultOption(): string|null - { - return 'callback'; + public function __construct( + public mixed $callback + ) { } /** diff --git a/src/DMS/Filter/Rules/Digits.php b/src/DMS/Filter/Rules/Digits.php index 47eb04a..c038bb4 100644 --- a/src/DMS/Filter/Rules/Digits.php +++ b/src/DMS/Filter/Rules/Digits.php @@ -8,19 +8,15 @@ /** * Digits Rule - * - * @Annotation */ -#[Attribute] +#[Attribute(Attribute::TARGET_PROPERTY)] class Digits extends RegExp { /** * Allow Whitespace or not */ - public bool $allowWhitespace = true; - - public function getDefaultOption(): string|null - { - return 'allowWhitespace'; + public function __construct( + public bool $allowWhitespace = true + ) { } } diff --git a/src/DMS/Filter/Rules/FloatScalar.php b/src/DMS/Filter/Rules/FloatScalar.php index f4f889a..6b07efb 100644 --- a/src/DMS/Filter/Rules/FloatScalar.php +++ b/src/DMS/Filter/Rules/FloatScalar.php @@ -9,10 +9,8 @@ /** * FloatScalar Rule * Converts content into a FloatScalar - * - * @Annotation */ -#[Attribute] +#[Attribute(Attribute::TARGET_PROPERTY)] class FloatScalar extends Rule { } diff --git a/src/DMS/Filter/Rules/HtmlEntities.php b/src/DMS/Filter/Rules/HtmlEntities.php index e4f924c..a8607b2 100644 --- a/src/DMS/Filter/Rules/HtmlEntities.php +++ b/src/DMS/Filter/Rules/HtmlEntities.php @@ -10,24 +10,19 @@ /** * Html Entities Filter - * - * @Annotation */ -#[Attribute] +#[Attribute(Attribute::TARGET_PROPERTY)] class HtmlEntities extends Rule { /** - * Flags + * @param int $flags Flags + * @param string $encoding Encoding to be used + * @param bool $doubleEncode Convert existing entities */ - public int $flags = ENT_COMPAT; - - /** - * Encoding to be used - */ - public string $encoding = 'UTF-8'; - - /** - * Convert existing entities - */ - public bool $doubleEncode = true; + public function __construct( + public int $flags = ENT_COMPAT, + public string $encoding = 'UTF-8', + public bool $doubleEncode = true + ) { + } } diff --git a/src/DMS/Filter/Rules/IntScalar.php b/src/DMS/Filter/Rules/IntScalar.php index 263b888..403d651 100644 --- a/src/DMS/Filter/Rules/IntScalar.php +++ b/src/DMS/Filter/Rules/IntScalar.php @@ -9,10 +9,8 @@ /** * IntScalar Rule * Converts content into an IntScalar - * - * @Annotation */ -#[Attribute] +#[Attribute(Attribute::TARGET_PROPERTY)] class IntScalar extends Rule { } diff --git a/src/DMS/Filter/Rules/Laminas.php b/src/DMS/Filter/Rules/Laminas.php index 6d46bd9..5c43733 100644 --- a/src/DMS/Filter/Rules/Laminas.php +++ b/src/DMS/Filter/Rules/Laminas.php @@ -10,26 +10,17 @@ * Laminas Rule * * Allows the use for Laminas Filters - * - * @Annotation */ -#[Attribute] +#[Attribute(Attribute::TARGET_PROPERTY)] class Laminas extends Rule { /** - * Laminas\Filter class, can be either a FQN or just Boolean for example + * @param string $class can be either a FQN or just Boolean for example + * @param array $laminasOptions Array of options to be passed into the Laminas Filter */ - public string $class; - - /** - * Array of options to be passed into the Laminas Filter - * - * @var mixed[] - */ - public array $laminasOptions = []; - - public function getDefaultOption(): string|null - { - return 'class'; + public function __construct( + public string $class, + public array $laminasOptions = [] + ) { } } diff --git a/src/DMS/Filter/Rules/PregReplace.php b/src/DMS/Filter/Rules/PregReplace.php index b48496c..ef55259 100644 --- a/src/DMS/Filter/Rules/PregReplace.php +++ b/src/DMS/Filter/Rules/PregReplace.php @@ -10,24 +10,17 @@ * PregReplace Rule * Replaces based on regular expression, will replace with empty if no * replacement is defined. - * - * @Annotation */ -#[Attribute] +#[Attribute(Attribute::TARGET_PROPERTY)] class PregReplace extends Rule { /** - * Regular Expression to use + * @param string|null $regexp Regular Expression to use + * @param string $replacement Replacement */ - public string|null $regexp = null; - - /** - * Replacement - */ - public string $replacement = ''; - - public function getDefaultOption(): string|null - { - return 'regexp'; + public function __construct( + public string|null $regexp = null, + public string $replacement = '', + ) { } } diff --git a/src/DMS/Filter/Rules/RegExp.php b/src/DMS/Filter/Rules/RegExp.php index 56ec148..69b2c43 100644 --- a/src/DMS/Filter/Rules/RegExp.php +++ b/src/DMS/Filter/Rules/RegExp.php @@ -10,10 +10,8 @@ * RegExp Rule * * Filter using preg_replace and unicode or non-unicode patterns - * - * @Annotation */ -#[Attribute] +#[Attribute(Attribute::TARGET_PROPERTY)] class RegExp extends Rule { /** diff --git a/src/DMS/Filter/Rules/Rule.php b/src/DMS/Filter/Rules/Rule.php index dbf710f..b088f4d 100644 --- a/src/DMS/Filter/Rules/Rule.php +++ b/src/DMS/Filter/Rules/Rule.php @@ -4,20 +4,6 @@ namespace DMS\Filter\Rules; -use DMS\Filter\Exception\InvalidOptionsException; -use DMS\Filter\Exception\MissingOptionsException; -use DMS\Filter\Exception\RuleDefinitionException; -use stdClass; - -use function array_flip; -use function array_keys; -use function count; -use function implode; -use function is_array; -use function is_string; -use function key; -use function property_exists; -use function sprintf; use function str_replace; /** @@ -28,152 +14,6 @@ */ abstract class Rule { - /** - * Initializes the filter rule with its options - * - * @param mixed $options The options (as associative array) - * or the value for the default - * option (any other type) - * - * @throws InvalidOptionsException When you pass the names of non-existing - * options - * @throws MissingOptionsException When you don't pass any of the options - * returned by getRequiredOptions() - */ - public function __construct(mixed $options = null) - { - $result = $this->parseOptions($options); - - if (count($result->invalidOptions) > 0) { - throw new InvalidOptionsException( - sprintf( - 'The options "%s" do not exist in rule %s', - implode('", "', $result->invalidOptions), - static::class, - ), - $result->invalidOptions, - ); - } - - if (count($result->missingOptions) > 0) { - throw new MissingOptionsException( - sprintf( - 'The options "%s" must be set for rule %s', - implode('", "', array_keys($result->missingOptions)), - static::class, - ), - array_keys($result->missingOptions), - ); - } - } - - /** - * Parses provided options into their properties and returns results - * for the parsing process - */ - private function parseOptions(mixed $options): stdClass - { - $parseResult = new stdClass(); - $parseResult->invalidOptions = []; - $parseResult->missingOptions = array_flip($this->getRequiredOptions()); - $options = $this->extractFromValueOption($options); - - if ($options === null) { - return $parseResult; - } - - //Parse Option Array - if ($this->isNonEmptyMap($options)) { - $this->parseOptionsArray($options, $parseResult); - - return $parseResult; - } - - //Parse Single Value - if ($options !== []) { - $this->parseSingleOption($options, $parseResult); - - return $parseResult; - } - - return $parseResult; - } - - /** - * Parses Options in the array format - * - * @param mixed[] $options - */ - private function parseOptionsArray(array $options, stdClass $result): void - { - foreach ($options as $option => $value) { - if (! property_exists($this, $option)) { - $result->invalidOptions[] = $option; - continue; - } - - //Define Option - $this->$option = $value; - unset($result->missingOptions[$option]); - } - } - - /** - * Parses single option received - * - * @param string|mixed[]|scalar $options - * - * @throws RuleDefinitionException - */ - private function parseSingleOption(mixed $options, stdClass $result): void - { - $option = $this->getDefaultOption(); - - //No Default set, unsure what to do - if ($option === null) { - throw new RuleDefinitionException( - sprintf('No default option is configured for rule %s', static::class), - ); - } - - //Default option points to invalid one - if (! property_exists($this, $option)) { - $result->invalidOptions[] = $option; - - return; - } - - //Define Option - $this->$option = $options; - unset($result->missingOptions[$option]); - } - - /** - * Returns the name of the required options - * - * Override this method if you want to define required options. - * - * @see __construct() - * - * @return string[] - */ - public function getRequiredOptions(): array - { - return []; - } - - /** - * Returns the name of the default option - * - * Override this method to define a default option. - * - * @see __construct() - */ - public function getDefaultOption(): string|null - { - return null; - } - /** * Retrieves the Filter class that is responsible for executing this filter * It may also be a service name. By default it loads a class with the @@ -183,21 +23,4 @@ public function getFilter(): string { return str_replace('Rules', 'Filters', static::class); } - - /** - * Doctrine parses constructor parameter into 'value' array param, restore it - */ - private function extractFromValueOption(mixed $options): mixed - { - if (is_array($options) && count($options) === 1 && isset($options['value'])) { - $options = $options['value']; - } - - return $options; - } - - private function isNonEmptyMap(mixed $options): bool - { - return is_array($options) && count($options) > 0 && is_string(key($options)); - } } diff --git a/src/DMS/Filter/Rules/StripNewlines.php b/src/DMS/Filter/Rules/StripNewlines.php index 419742c..ce256af 100644 --- a/src/DMS/Filter/Rules/StripNewlines.php +++ b/src/DMS/Filter/Rules/StripNewlines.php @@ -8,10 +8,8 @@ /** * StripNewlines Rule - * - * @Annotation */ -#[Attribute] +#[Attribute(Attribute::TARGET_PROPERTY)] class StripNewlines extends Rule { } diff --git a/src/DMS/Filter/Rules/StripTags.php b/src/DMS/Filter/Rules/StripTags.php index 2b2edff..d1c3658 100644 --- a/src/DMS/Filter/Rules/StripTags.php +++ b/src/DMS/Filter/Rules/StripTags.php @@ -8,19 +8,15 @@ /** * StripTags Rule - * - * @Annotation */ -#[Attribute] +#[Attribute(Attribute::TARGET_PROPERTY)] class StripTags extends Rule { /** * String of allowed tags. Ex: */ - public string|null $allowed = null; - - public function getDefaultOption(): string|null - { - return 'allowed'; + public function __construct( + public string|null $allowed = null + ) { } } diff --git a/src/DMS/Filter/Rules/ToLower.php b/src/DMS/Filter/Rules/ToLower.php index e1c1391..4ef9d62 100644 --- a/src/DMS/Filter/Rules/ToLower.php +++ b/src/DMS/Filter/Rules/ToLower.php @@ -8,19 +8,15 @@ /** * ToLower Rule - * - * @Annotation */ -#[Attribute] +#[Attribute(Attribute::TARGET_PROPERTY)] class ToLower extends Rule { /** * Encoding to be used */ - public string|null $encoding = null; - - public function getDefaultOption(): string|null - { - return 'encoding'; + public function __construct( + public string|null $encoding = null + ) { } } diff --git a/src/DMS/Filter/Rules/ToUpper.php b/src/DMS/Filter/Rules/ToUpper.php index 151bf8d..1288336 100644 --- a/src/DMS/Filter/Rules/ToUpper.php +++ b/src/DMS/Filter/Rules/ToUpper.php @@ -8,19 +8,15 @@ /** * ToUpper Rule - * - * @Annotation */ -#[Attribute] +#[Attribute(Attribute::TARGET_PROPERTY)] class ToUpper extends Rule { /** * Encoding to be used */ - public string|null $encoding = null; - - public function getDefaultOption(): string|null - { - return 'encoding'; + public function __construct( + public string|null $encoding = null + ) { } } diff --git a/src/DMS/Filter/Rules/Trim.php b/src/DMS/Filter/Rules/Trim.php index a3aa881..ed49cdc 100644 --- a/src/DMS/Filter/Rules/Trim.php +++ b/src/DMS/Filter/Rules/Trim.php @@ -8,19 +8,15 @@ /** * Trim Rule - * - * @Annotation */ -#[Attribute] +#[Attribute(Attribute::TARGET_PROPERTY)] class Trim extends Rule { /** * Comma separated string of allowed tags */ - public string|null $charlist = null; - - public function getDefaultOption(): string|null - { - return 'charlist'; + public function __construct( + public string|null $charlist = null + ) { } } From 9ab3e1f597fe645e7e81da8faa1fdb33e58b1ee7 Mon Sep 17 00:00:00 2001 From: Igor Kusmitsch Date: Wed, 25 Sep 2024 16:21:24 -0300 Subject: [PATCH 07/15] fix PHP return types and phpcs warnings --- src/DMS/Filter/Filters/BooleanScalar.php | 2 +- src/DMS/Filter/Filters/FloatScalar.php | 2 +- src/DMS/Filter/Filters/IntScalar.php | 2 +- src/DMS/Filter/Filters/RegExp.php | 5 ++--- src/DMS/Filter/Filters/ToLower.php | 2 +- src/DMS/Filter/Filters/ToUpper.php | 5 ++--- 6 files changed, 8 insertions(+), 10 deletions(-) diff --git a/src/DMS/Filter/Filters/BooleanScalar.php b/src/DMS/Filter/Filters/BooleanScalar.php index 602448e..e54c289 100644 --- a/src/DMS/Filter/Filters/BooleanScalar.php +++ b/src/DMS/Filter/Filters/BooleanScalar.php @@ -14,7 +14,7 @@ class BooleanScalar extends BaseFilter /** * {@inheritDoc} */ - public function apply(Rule $rule, $value): mixed + public function apply(Rule $rule, $value): bool { return (bool) $value; } diff --git a/src/DMS/Filter/Filters/FloatScalar.php b/src/DMS/Filter/Filters/FloatScalar.php index 03ac5d6..502db11 100644 --- a/src/DMS/Filter/Filters/FloatScalar.php +++ b/src/DMS/Filter/Filters/FloatScalar.php @@ -18,7 +18,7 @@ class FloatScalar extends BaseFilter /** * {@inheritDoc} */ - public function apply(Rule $rule, $value): mixed + public function apply(Rule $rule, $value): ?float { if (is_array($value) || is_object($value)) { return null; diff --git a/src/DMS/Filter/Filters/IntScalar.php b/src/DMS/Filter/Filters/IntScalar.php index 8dc11c9..4a0fc10 100644 --- a/src/DMS/Filter/Filters/IntScalar.php +++ b/src/DMS/Filter/Filters/IntScalar.php @@ -15,7 +15,7 @@ class IntScalar extends BaseFilter /** * {@inheritDoc} */ - public function apply(Rule $rule, $value): mixed + public function apply(Rule $rule, $value): int { return (int) ($value); } diff --git a/src/DMS/Filter/Filters/RegExp.php b/src/DMS/Filter/Filters/RegExp.php index bc4d7bc..b62ee0a 100644 --- a/src/DMS/Filter/Filters/RegExp.php +++ b/src/DMS/Filter/Filters/RegExp.php @@ -30,7 +30,7 @@ class RegExp extends BaseFilter public function apply(Rule $rule, $value): mixed { //Build pattern - $pattern = $this->checkUnicodeSupport() && $rule->unicodePattern !== null + $pattern = $this->checkUnicodeSupport() ? $rule->unicodePattern : $rule->pattern; @@ -43,8 +43,7 @@ public function apply(Rule $rule, $value): mixed public function checkUnicodeSupport(): bool { if (! isset(static::$unicodeEnabled)) { - //phpcs:disable SlevomatCodingStandard.ControlStructures.UselessTernaryOperator.UselessTernaryOperator - static::$unicodeEnabled = @preg_match('/\pL/u', 'a') ? true : false; + static::$unicodeEnabled = (bool)(@preg_match('/\pL/u', 'a')); } return static::$unicodeEnabled; diff --git a/src/DMS/Filter/Filters/ToLower.php b/src/DMS/Filter/Filters/ToLower.php index deb685b..1fc7c1c 100644 --- a/src/DMS/Filter/Filters/ToLower.php +++ b/src/DMS/Filter/Filters/ToLower.php @@ -24,7 +24,7 @@ class ToLower extends BaseFilter * * @param \DMS\Filter\Rules\ToLower $rule */ - public function apply(Rule $rule, $value): mixed + public function apply(Rule $rule, $value): string { if ($this->useEncoding($rule)) { return mb_strtolower((string) $value, $rule->encoding); diff --git a/src/DMS/Filter/Filters/ToUpper.php b/src/DMS/Filter/Filters/ToUpper.php index 2b1670c..72efd31 100644 --- a/src/DMS/Filter/Filters/ToUpper.php +++ b/src/DMS/Filter/Filters/ToUpper.php @@ -25,7 +25,7 @@ class ToUpper extends BaseFilter * * @param \DMS\Filter\Rules\ToUpper $rule */ - public function apply(Rule $rule, $value): mixed + public function apply(Rule $rule, $value): string { if ($this->useEncoding($rule)) { return mb_strtoupper((string) $value, $rule->encoding); @@ -52,8 +52,7 @@ public function useEncoding(\DMS\Filter\Rules\ToUpper $rule): bool ); } - $this->encoding = (string) $rule->encoding; - $encodings = array_map('strtolower', mb_list_encodings()); + $encodings = array_map('strtolower', mb_list_encodings()); if (! in_array(strtolower($rule->encoding), $encodings)) { throw new FilterException( From 37f5239074c0bf77b49caa53c7e921e9f1c10088 Mon Sep 17 00:00:00 2001 From: Igor Kusmitsch Date: Wed, 25 Sep 2024 16:22:21 -0300 Subject: [PATCH 08/15] fix phpcs warnings --- src/DMS/Filter/Filter.php | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/DMS/Filter/Filter.php b/src/DMS/Filter/Filter.php index 32ae019..3e846ab 100644 --- a/src/DMS/Filter/Filter.php +++ b/src/DMS/Filter/Filter.php @@ -4,15 +4,16 @@ namespace DMS\Filter; -/** - * Filter Object, responsible for retrieving the filtering rules - * for the object and applying them - */ use DMS\Filter\Filters\Loader\FilterLoaderInterface; use DMS\Filter\Mapping\ClassMetadataFactoryInterface; use DMS\Filter\Rules\Rule; use ReflectionException; +/** + * Filter Object, responsible for retrieving the filtering rules + * for the object and applying them + */ + /** * Executor, receives objects that need filtering and executes attached rules. */ @@ -21,8 +22,10 @@ class Filter implements FilterInterface /** * Constructor */ - public function __construct(protected Mapping\ClassMetadataFactory $metadataFactory, protected FilterLoaderInterface $filterLoader) - { + public function __construct( + protected Mapping\ClassMetadataFactory $metadataFactory, + protected FilterLoaderInterface $filterLoader + ) { } /** From e847f8dfca876ed000ce62618c52437e6d71d5be Mon Sep 17 00:00:00 2001 From: Igor Kusmitsch Date: Wed, 25 Sep 2024 16:24:07 -0300 Subject: [PATCH 09/15] Update for using symfony cache adapter instead of doctrine cache --- src/DMS/Filter/Mapping/ClassMetadata.php | 2 +- .../Filter/Mapping/ClassMetadataFactory.php | 21 ++++++++++++------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/DMS/Filter/Mapping/ClassMetadata.php b/src/DMS/Filter/Mapping/ClassMetadata.php index 26f8a6d..3913ee8 100644 --- a/src/DMS/Filter/Mapping/ClassMetadata.php +++ b/src/DMS/Filter/Mapping/ClassMetadata.php @@ -11,7 +11,7 @@ use function array_keys; /** - * Represents a class that has Annotations + * Represents a class that has Attributes */ class ClassMetadata implements ClassMetadataInterface { diff --git a/src/DMS/Filter/Mapping/ClassMetadataFactory.php b/src/DMS/Filter/Mapping/ClassMetadataFactory.php index fc2c5e6..6f17080 100644 --- a/src/DMS/Filter/Mapping/ClassMetadataFactory.php +++ b/src/DMS/Filter/Mapping/ClassMetadataFactory.php @@ -4,7 +4,8 @@ namespace DMS\Filter\Mapping; -use Doctrine\Common\Cache\Cache; +use Psr\Cache\InvalidArgumentException; +use Symfony\Component\Cache\Adapter\AdapterInterface; use function ltrim; @@ -20,12 +21,13 @@ class ClassMetadataFactory implements ClassMetadataFactoryInterface * Constructor * Receives a Loader and a Doctrine Compatible cache instance */ - public function __construct(protected Loader\LoaderInterface $loader, protected Cache|null $cache = null) + public function __construct(protected Loader\LoaderInterface $loader, protected AdapterInterface|null $cache = null) { } /** * {@inheritDoc} + * @throws InvalidArgumentException */ public function getClassMetadata($class): ClassMetadataInterface { @@ -37,8 +39,8 @@ public function getClassMetadata($class): ClassMetadataInterface } //Check Cache for it - if ($this->cache !== null && $this->cache->contains($class)) { - $this->setParsedClass($class, $this->cache->fetch($class)); + if ($this->cache !== null && $this->cache->getItem($class)->isHit()) { + $this->setParsedClass($class, $this->cache->getItem($class)->get()); return $this->getParsedClass($class); } @@ -49,6 +51,7 @@ public function getClassMetadata($class): ClassMetadataInterface /** * Reads class metadata for a new and unparsed class + * @throws InvalidArgumentException */ private function parseClassMetadata(string $class): ClassMetadataInterface { @@ -58,14 +61,16 @@ private function parseClassMetadata(string $class): ClassMetadataInterface $this->loadParentMetadata($metadata); $this->loadInterfaceMetadata($metadata); - //Load Annotations from Reader + //Load Attributes from Reader $this->loader->loadClassMetadata($metadata); //Store internally $this->setParsedClass($class, $metadata); if ($this->cache !== null) { - $this->cache->save($class, $metadata); + $cachedClass = $this->cache->getItem($class); + $cachedClass->set($metadata); + $this->cache->save($cachedClass); } return $metadata; @@ -102,6 +107,7 @@ private function setParsedClass(string $class, ClassMetadataInterface $metadata) /** * Checks if the class being parsed has a parent and cascades parsing * to its parent + * @throws InvalidArgumentException */ protected function loadParentMetadata(ClassMetadataInterface $metadata): void { @@ -115,8 +121,9 @@ protected function loadParentMetadata(ClassMetadataInterface $metadata): void } /** - * Checks if the object has interfaces and cascades parsing of annotatiosn + * Checks if the object has interfaces and cascades parsing of attributes * to all the interfaces + * @throws InvalidArgumentException */ protected function loadInterfaceMetadata(ClassMetadataInterface $metadata): void { From baf89bcf9d457e3b6fbdb1ede3d0bfc40467fae8 Mon Sep 17 00:00:00 2001 From: Igor Kusmitsch Date: Wed, 25 Sep 2024 16:24:36 -0300 Subject: [PATCH 10/15] fix tests --- tests/DMS/Filter/FilterTest.php | 59 +++++------------- tests/DMS/Filter/Filters/AlnumTest.php | 52 +++++++--------- tests/DMS/Filter/Filters/AlphaTest.php | 48 +++++++-------- tests/DMS/Filter/Filters/BooleanTest.php | 27 ++++----- tests/DMS/Filter/Filters/CallbackTest.php | 33 ++++------ tests/DMS/Filter/Filters/DigitsTest.php | 42 ++++++------- tests/DMS/Filter/Filters/FloatTest.php | 23 +++---- tests/DMS/Filter/Filters/HtmlEntitiesTest.php | 27 ++++----- tests/DMS/Filter/Filters/IntTest.php | 29 ++++----- tests/DMS/Filter/Filters/LaminasTest.php | 31 +++++----- .../Filters/Loader/FilterLoaderTest.php | 13 ++-- tests/DMS/Filter/Filters/PregReplaceTest.php | 27 ++++----- .../DMS/Filter/Filters/StripNewlinesTest.php | 28 +++------ tests/DMS/Filter/Filters/StripTagsTest.php | 27 ++++----- tests/DMS/Filter/Filters/ToLowerTest.php | 34 ++++------- tests/DMS/Filter/Filters/ToUpperTest.php | 34 ++++------- tests/DMS/Filter/Filters/TrimTest.php | 29 ++++----- .../Mapping/ClassMetadataFactoryTest.php | 39 +++++------- .../Mapping/Loader/AttributeLoaderTest.php | 60 +++++++++---------- tests/DMS/Filter/Rules/CallbackTest.php | 20 ++----- tests/DMS/Filter/Rules/RuleTest.php | 44 -------------- .../Tests/Dummy/Classes/AttributedClass.php | 8 +-- .../Tests/Dummy/Rules/MultipleOptionsRule.php | 1 - tests/DMS/Tests/FilterTestCase.php | 12 +--- 24 files changed, 271 insertions(+), 476 deletions(-) diff --git a/tests/DMS/Filter/FilterTest.php b/tests/DMS/Filter/FilterTest.php index f8c09c5..e93a126 100644 --- a/tests/DMS/Filter/FilterTest.php +++ b/tests/DMS/Filter/FilterTest.php @@ -7,12 +7,11 @@ use DMS\Tests\Dummy; use DMS\Filter\Mapping\ClassMetadataFactory; use Generator; +use PHPUnit\Framework\Attributes\DataProvider; class FilterTest extends FilterTestCase { - /** - * @dataProvider filterClassDataProvider - */ + #[DataProvider('filterClassDataProvider')] public function testFilter(Filter $filter, $class): void { $class->name = "Sir Isaac Newton"; @@ -31,9 +30,7 @@ public function testFilter(Filter $filter, $class): void $this->assertStringNotContainsString("

", $class->description); } - /** - * @dataProvider filterChildClassDataProvider - */ + #[DataProvider('filterChildClassDataProvider')] public function testFilterWithParent(Filter $filter, $class): void { $class->name = "Sir Isaac Newton"; @@ -55,9 +52,7 @@ public function testFilterWithParent(Filter $filter, $class): void $this->assertStringNotContainsString(" ", $class->surname); } - /** - * @dataProvider filterClassDataProvider - */ + #[DataProvider('filterClassDataProvider')] public function testFilterProperty(Filter $filter, $class): void { $class->name = "Sir Isaac Newton"; @@ -74,9 +69,7 @@ public function testFilterProperty(Filter $filter, $class): void $this->assertStringNotContainsString("

", $class->description); } - /** - * @dataProvider filterDataProvider - */ + #[DataProvider('filterDataProvider')] public function testFilterValue(Filter $filter): void { $value = "this is a string

with tags

and malformed"; @@ -89,9 +82,7 @@ public function testFilterValue(Filter $filter): void $this->assertStringNotContainsString('

', $filtered); } - /** - * @dataProvider filterDataProvider - */ + #[DataProvider('filterDataProvider')] public function testFilterValueWithArray(Filter $filter): void { $value = "this is a string

with tags

and\n malformed"; @@ -106,57 +97,39 @@ public function testFilterValueWithArray(Filter $filter): void $this->assertStringNotContainsString('\n', $filtered); } - /** - * @dataProvider filterDataProvider - */ + #[DataProvider('filterDataProvider')] public function testNotFailOnNull(Filter $filter): void { $this->expectNotToPerformAssertions(); $filter->filterEntity(null); } - /** - * @dataProvider filterDataProvider - */ + #[DataProvider('filterDataProvider')] public function testGetMetadataFactory(Filter $filter): void { $this->assertInstanceOf(ClassMetadataFactory::class, $filter->getMetadataFactory()); } - - public function filterClassDataProvider(): Generator - { - yield 'Annotation' => [ - new Filter($this->buildMetadataFactoryWithAnnotationLoader(), new FilterLoader()), - new Dummy\Classes\AnnotatedClass(), - ]; + public static function filterClassDataProvider(): Generator + { yield 'Attribute' => [ - new Filter($this->buildMetadataFactoryWithAttributeLoader(), new FilterLoader()), + new Filter(self::buildMetadataFactoryWithAttributeLoader(), new FilterLoader()), new Dummy\Classes\AttributedClass(), ]; } - public function filterChildClassDataProvider(): Generator + public static function filterChildClassDataProvider(): Generator { - yield 'Annotation' => [ - new Filter($this->buildMetadataFactoryWithAnnotationLoader(), new FilterLoader()), - new Dummy\Classes\ChildAnnotatedClass(), - ]; - yield 'Attribute' => [ - new Filter($this->buildMetadataFactoryWithAttributeLoader(), new FilterLoader()), + new Filter(self::buildMetadataFactoryWithAttributeLoader(), new FilterLoader()), new Dummy\Classes\ChildAttributedClass(), ]; } - - public function filterDataProvider(): Generator - { - yield 'Annotation' => [ - new Filter($this->buildMetadataFactoryWithAnnotationLoader(), new FilterLoader()), - ]; + public static function filterDataProvider(): Generator + { yield 'Attribute' => [ - new Filter($this->buildMetadataFactoryWithAttributeLoader(), new FilterLoader()), + new Filter(self::buildMetadataFactoryWithAttributeLoader(), new FilterLoader()), ]; } } diff --git a/tests/DMS/Filter/Filters/AlnumTest.php b/tests/DMS/Filter/Filters/AlnumTest.php index d34ea02..530948f 100644 --- a/tests/DMS/Filter/Filters/AlnumTest.php +++ b/tests/DMS/Filter/Filters/AlnumTest.php @@ -4,24 +4,18 @@ use DMS\Tests\FilterTestCase; use DMS\Filter\Rules\Alnum as AlnumRule; +use PHPUnit\Framework\Attributes\DataProvider; +use ReflectionException; use ReflectionProperty; class AlnumTest extends FilterTestCase { - /** - * @dataProvider provideForRule - * - * @param $options - * @param $value - * @param $expectedResult - * @param null $unicodeSetting - * - * @throws \ReflectionException + * @throws ReflectionException */ - public function testRule($options, $value, $expectedResult, $unicodeSetting = null): void + #[DataProvider('provideForRule')] + public function testRule(AlnumRule $rule, $value, $expectedResult, $unicodeSetting = null): void { - $rule = new AlnumRule($options); $filter = new Alnum(); if ($unicodeSetting !== null) { @@ -35,26 +29,26 @@ public function testRule($options, $value, $expectedResult, $unicodeSetting = nu $this->assertEquals($expectedResult, $result); } - public function provideForRule(): array + public static function provideForRule(): array { return [ - [false, "My Text", "MyText", true], - [false, "My Text", "MyText", false], - [true, "My Text", "My Text", true], - [true, "My Text", "My Text", false], - [true, "My Text!", "My Text", true], - [true, "My Text!", "My Text", false], - [true, "My Text21!", "My Text21", true], - [true, "My Text21!", "My Text21", false], - [true, "João Sorrisão", "João Sorrisão", true], - [true, "João Sorrisão", "Joo Sorriso", false], - [true, "GRΣΣK", "GRΣΣK", true], - [true, "GRΣΣK", "GRK", false], - [true, "Helgi Þormar Þorbjörnsson", "Helgi Þormar Þorbjörnsson", true], - [true, "Helgi Þormar Þorbjörnsson", "Helgi ormar orbjrnsson", false], - [true, "Helgi Þormar!@#$&*( )(*&%$#@Þorbjörnsson", "Helgi Þormar Þorbjörnsson", true], - [true, "Helgi Þormar!@#$&*( )(*&%$#@Þorbjörnsson", "Helgi ormar orbjrnsson", false], - [true, null, null, false], + [new AlnumRule(false), "My Text", "MyText", true], + [new AlnumRule(false), "My Text", "MyText", false], + [new AlnumRule(true), "My Text", "My Text", true], + [new AlnumRule(true), "My Text", "My Text", false], + [new AlnumRule(true), "My Text!", "My Text", true], + [new AlnumRule(true), "My Text!", "My Text", false], + [new AlnumRule(true), "My Text21!", "My Text21", true], + [new AlnumRule(true), "My Text21!", "My Text21", false], + [new AlnumRule(true), "João Sorrisão", "João Sorrisão", true], + [new AlnumRule(true), "João Sorrisão", "Joo Sorriso", false], + [new AlnumRule(true), "GRΣΣK", "GRΣΣK", true], + [new AlnumRule(true), "GRΣΣK", "GRK", false], + [new AlnumRule(true), "Helgi Þormar Þorbjörnsson", "Helgi Þormar Þorbjörnsson", true], + [new AlnumRule(true), "Helgi Þormar Þorbjörnsson", "Helgi ormar orbjrnsson", false], + [new AlnumRule(true), "Helgi Þormar!@#$&*( )(*&%$#@Þorbjörnsson", "Helgi Þormar Þorbjörnsson", true], + [new AlnumRule(true), "Helgi Þormar!@#$&*( )(*&%$#@Þorbjörnsson", "Helgi ormar orbjrnsson", false], + [new AlnumRule(true), null, null, false], ]; } } diff --git a/tests/DMS/Filter/Filters/AlphaTest.php b/tests/DMS/Filter/Filters/AlphaTest.php index 0eb584a..48166f5 100644 --- a/tests/DMS/Filter/Filters/AlphaTest.php +++ b/tests/DMS/Filter/Filters/AlphaTest.php @@ -4,24 +4,18 @@ use DMS\Tests\FilterTestCase; use DMS\Filter\Rules\Alpha as AlphaRule; +use PHPUnit\Framework\Attributes\DataProvider; +use ReflectionException; use ReflectionProperty; class AlphaTest extends FilterTestCase { - /** - * @dataProvider provideForRule - * - * @param $options - * @param $value - * @param $expectedResult - * @param null $unicodeSetting - * - * @throws \ReflectionException + * @throws ReflectionException */ - public function testRule($options, $value, $expectedResult, $unicodeSetting = null): void + #[DataProvider('provideForRule')] + public function testRule(AlphaRule $rule, $value, $expectedResult, $unicodeSetting = null): void { - $rule = new AlphaRule($options); $filter = new Alpha(); if ($unicodeSetting !== null) { @@ -35,24 +29,24 @@ public function testRule($options, $value, $expectedResult, $unicodeSetting = nu $this->assertEquals($expectedResult, $result); } - public function provideForRule(): array + public static function provideForRule(): array { return [ - [false, "My Text", "MyText", true], - [false, "My Text", "MyText", false], - [true, "My Text", "My Text", true], - [true, "My Text", "My Text", false], - [true, "My Text!", "My Text", true], - [true, "My Text!", "My Text", false], - [true, "My Text21!", "My Text", true], - [true, "My Text21!", "My Text", false], - [true, "João 2Sorrisão", "João Sorrisão", true], - [true, "João 2Sorrisão", "Joo Sorriso", false], - [true, "Helgi Þormar Þorbjörnsson", "Helgi Þormar Þorbjörnsson", true], - [true, "Helgi Þormar Þorbjörnsson", "Helgi ormar orbjrnsson", false], - [true, "Helgi Þormar!@#$&*( )(*&%$#@Þorbjörnsson", "Helgi Þormar Þorbjörnsson", true], - [true, "Helgi Þormar!@#$&*( )(*&%$#@Þorbjörnsson", "Helgi ormar orbjrnsson", false], - [true, null, null, false], + [new AlphaRule(false), "My Text", "MyText", true], + [new AlphaRule(false), "My Text", "MyText", false], + [new AlphaRule(true), "My Text", "My Text", true], + [new AlphaRule(true), "My Text", "My Text", false], + [new AlphaRule(true), "My Text!", "My Text", true], + [new AlphaRule(true), "My Text!", "My Text", false], + [new AlphaRule(true), "My Text21!", "My Text", true], + [new AlphaRule(true), "My Text21!", "My Text", false], + [new AlphaRule(true), "João 2Sorrisão", "João Sorrisão", true], + [new AlphaRule(true), "João 2Sorrisão", "Joo Sorriso", false], + [new AlphaRule(true), "Helgi Þormar Þorbjörnsson", "Helgi Þormar Þorbjörnsson", true], + [new AlphaRule(true), "Helgi Þormar Þorbjörnsson", "Helgi ormar orbjrnsson", false], + [new AlphaRule(true), "Helgi Þormar!@#$&*( )(*&%$#@Þorbjörnsson", "Helgi Þormar Þorbjörnsson", true], + [new AlphaRule(true), "Helgi Þormar!@#$&*( )(*&%$#@Þorbjörnsson", "Helgi ormar orbjrnsson", false], + [new AlphaRule(true), null, null, false], ]; } } diff --git a/tests/DMS/Filter/Filters/BooleanTest.php b/tests/DMS/Filter/Filters/BooleanTest.php index a5aa79b..b60eab2 100644 --- a/tests/DMS/Filter/Filters/BooleanTest.php +++ b/tests/DMS/Filter/Filters/BooleanTest.php @@ -4,20 +4,13 @@ use DMS\Tests\FilterTestCase; use DMS\Filter\Rules\BooleanScalar as BooleanRule; +use PHPUnit\Framework\Attributes\DataProvider; class BooleanTest extends FilterTestCase { - - /** - * @dataProvider provideForRule - * - * @param $options - * @param $value - * @param $expectedResult - */ - public function testRule($options, $value, $expectedResult): void + #[DataProvider('provideForRule')] + public function testRule(BooleanRule $rule, $value, $expectedResult): void { - $rule = new BooleanRule($options); $filter = new BooleanScalar(); $result = $filter->apply($rule, $value); @@ -25,15 +18,15 @@ public function testRule($options, $value, $expectedResult): void $this->assertEquals($expectedResult, $result); } - public function provideForRule(): array + public static function provideForRule(): array { return [ - [null, "My Text", true], - [null, "", false], - [null, null, false], - [null, 21.9, true], - [null, 21, true], - [null, 0, false], + [new BooleanRule(), "My Text", true], + [new BooleanRule(), "", false], + [new BooleanRule(), null, false], + [new BooleanRule(), 21.9, true], + [new BooleanRule(), 21, true], + [new BooleanRule(), 0, false], ]; } } diff --git a/tests/DMS/Filter/Filters/CallbackTest.php b/tests/DMS/Filter/Filters/CallbackTest.php index cf356da..c4be536 100644 --- a/tests/DMS/Filter/Filters/CallbackTest.php +++ b/tests/DMS/Filter/Filters/CallbackTest.php @@ -4,28 +4,21 @@ use DMS\Filter\Exception\FilterException; use DMS\Filter\Exception\InvalidCallbackException; -use DMS\Tests\Dummy\Classes\AnnotatedClass; +use DMS\Tests\Dummy\Classes\AttributedClass; use DMS\Tests\FilterTestCase; use DMS\Filter\Rules\Callback as CallbackRule; use PHPUnit\Framework\MockObject\MockObject; class CallbackTest extends FilterTestCase { - /** - * @var CallbackRule|MockObject - */ - protected $rule; - - /** - * @var Callback - */ - protected $filter; + protected MockObject|CallbackRule $rule; + protected Callback $filter; public function setUp(): void -{ + { parent::setUp(); - $this->rule = $this->getMockBuilder(CallbackRule::class)->getMock(); + $this->rule = $this->getMockBuilder(CallbackRule::class)->disableOriginalConstructor()->getMock(); $this->filter = new Callback(); } @@ -36,7 +29,7 @@ public function testRuleWithObjectMethod(): void ); $this->rule->callback = 'callbackMethod'; - $obj = new AnnotatedClass(); + $obj = new AttributedClass(); $this->filter->setCurrentObject($obj); $result = $this->filter->apply($this->rule, 'value'); @@ -52,7 +45,7 @@ public function testRuleWithObjectMethodInvalid(): void ); $this->rule->callback = 'callbackMissingMethod'; - $obj = new AnnotatedClass(); + $obj = new AttributedClass(); $this->filter->setCurrentObject($obj); $this->filter->apply($this->rule, 'value'); @@ -74,7 +67,7 @@ public function testRuleWithCallable(): void $this->rule->expects($this->once())->method('getInputType')->willReturn( CallbackRule::CALLABLE_TYPE ); - $this->rule->callback = [AnnotatedClass::class, 'anotherCallback']; + $this->rule->callback = [AttributedClass::class, 'anotherCallback']; $result = $this->filter->apply($this->rule, 'value'); @@ -86,7 +79,7 @@ public function testRuleWithNonStaticCallable(): void $this->rule->expects($this->once())->method('getInputType')->willReturn( CallbackRule::CALLABLE_TYPE ); - $this->rule->callback = [new AnnotatedClass(), 'callbackMethod']; + $this->rule->callback = [new AttributedClass(), 'callbackMethod']; $result = $this->filter->apply($this->rule, 'value'); @@ -99,14 +92,14 @@ public function testRuleWithCallableInvalid(): void $this->rule->expects($this->once())->method('getInputType')->willReturn( CallbackRule::CALLABLE_TYPE ); - $this->rule->callback = [AnnotatedClass::class, 'callbackMissingMethod']; + $this->rule->callback = [AttributedClass::class, 'callbackMissingMethod']; - $result = $this->filter->apply($this->rule, 'value'); + $this->filter->apply($this->rule, 'value'); } public function testRuleWithClosure(): void { - $closure = static function ($value) { + $closure = static function () { return 'called_back'; }; @@ -128,6 +121,6 @@ public function testRuleWithNonClosure(): void ); $this->rule->callback = "i'm not a closure"; - $result = $this->filter->apply($this->rule, 'value'); + $this->filter->apply($this->rule, 'value'); } } diff --git a/tests/DMS/Filter/Filters/DigitsTest.php b/tests/DMS/Filter/Filters/DigitsTest.php index ed7aabe..2165a3f 100644 --- a/tests/DMS/Filter/Filters/DigitsTest.php +++ b/tests/DMS/Filter/Filters/DigitsTest.php @@ -4,24 +4,18 @@ use DMS\Tests\FilterTestCase; use DMS\Filter\Rules\Digits as DigitsRule; +use PHPUnit\Framework\Attributes\DataProvider; +use ReflectionException; use ReflectionProperty; class DigitsTest extends FilterTestCase { - /** - * @dataProvider provideForRule - * - * @param $options - * @param $value - * @param $expectedResult - * @param null $unicodeSetting - * - * @throws \ReflectionException + * @throws ReflectionException */ - public function testRule($options, $value, $expectedResult, $unicodeSetting = null): void + #[DataProvider('provideForRule')] + public function testRule(DigitsRule $rule, $value, $expectedResult, $unicodeSetting = null): void { - $rule = new DigitsRule($options); $filter = new Digits(); if ($unicodeSetting !== null) { @@ -35,21 +29,21 @@ public function testRule($options, $value, $expectedResult, $unicodeSetting = nu $this->assertEquals($expectedResult, $result); } - public function provideForRule(): array + public static function provideForRule(): array { return [ - [false, "My Text", ""], - [false, "001 t55", "00155"], - [true, "My 23 dogs", " 23 "], - [false, "My 23 dogs", "23"], - [true, "233 055", "233 055", true], - [true, "233 055", "233 055", false], - [true, "233 t055s", "233 055"], - [true, "My Text21!", " 21"], //TODO verify this. - [true, "João Sorrisão", " ", true], - [true, "João Sorrisão", " ", false], - [true, "001Helgi Þormar Þorbjörnsson", "001 ", true], - [true, "001Helgi Þormar Þorbjörnsson", "001 ", false], + [new DigitsRule(false), "My Text", ""], + [new DigitsRule(false), "001 t55", "00155"], + [new DigitsRule(true), "My 23 dogs", " 23 "], + [new DigitsRule(false), "My 23 dogs", "23"], + [new DigitsRule(true), "233 055", "233 055", true], + [new DigitsRule(true), "233 055", "233 055", false], + [new DigitsRule(true), "233 t055s", "233 055"], + [new DigitsRule(true), "My Text21!", " 21"], //TODO verify this. + [new DigitsRule(true), "João Sorrisão", " ", true], + [new DigitsRule(true), "João Sorrisão", " ", false], + [new DigitsRule(true), "001Helgi Þormar Þorbjörnsson", "001 ", true], + [new DigitsRule(true), "001Helgi Þormar Þorbjörnsson", "001 ", false], ]; } } diff --git a/tests/DMS/Filter/Filters/FloatTest.php b/tests/DMS/Filter/Filters/FloatTest.php index 2391f00..b73a06c 100644 --- a/tests/DMS/Filter/Filters/FloatTest.php +++ b/tests/DMS/Filter/Filters/FloatTest.php @@ -4,20 +4,13 @@ use DMS\Tests\FilterTestCase; use DMS\Filter\Rules\FloatScalar as FloatRule; +use PHPUnit\Framework\Attributes\DataProvider; class FloatTest extends FilterTestCase { - - /** - * @dataProvider provideForRule - * - * @param $options - * @param $value - * @param $expectedResult - */ - public function testRule($options, $value, $expectedResult): void + #[DataProvider('provideForRule')] + public function testRule(FloatRule $rule, $value, $expectedResult): void { - $rule = new FloatRule($options); $filter = new FloatScalar(); $result = $filter->apply($rule, $value); @@ -25,13 +18,13 @@ public function testRule($options, $value, $expectedResult): void $this->assertEquals($expectedResult, $result); } - public function provideForRule(): array + public static function provideForRule(): array { return [ - [null, "My Text", 0.0], - [null, "21", 21.0], - [null, "21.2", 21.2], - [null, 21.9, 21.9], + [new FloatRule(), "My Text", 0.0], + [new FloatRule(), "21", 21.0], + [new FloatRule(), "21.2", 21.2], + [new FloatRule(), 21.9, 21.9], ]; } } diff --git a/tests/DMS/Filter/Filters/HtmlEntitiesTest.php b/tests/DMS/Filter/Filters/HtmlEntitiesTest.php index e9bd660..2342337 100644 --- a/tests/DMS/Filter/Filters/HtmlEntitiesTest.php +++ b/tests/DMS/Filter/Filters/HtmlEntitiesTest.php @@ -4,20 +4,13 @@ use DMS\Tests\FilterTestCase; use DMS\Filter\Rules\HtmlEntities as HtmlEntitiesRule; +use PHPUnit\Framework\Attributes\DataProvider; class HtmlEntitiesTest extends FilterTestCase { - - /** - * @dataProvider provideForRule - * - * @param $options - * @param $value - * @param $expectedResult - */ - public function testRule($options, $value, $expectedResult): void + #[DataProvider('provideForRule')] + public function testRule(HtmlEntitiesRule $rule, $value, $expectedResult): void { - $rule = new HtmlEntitiesRule($options); $filter = new HtmlEntities(); $result = $filter->apply($rule, $value); @@ -25,15 +18,15 @@ public function testRule($options, $value, $expectedResult): void $this->assertEquals($expectedResult, $result); } - public function provideForRule(): array + public static function provideForRule(): array { return [ - [[], "This is some téxt &", "This is some téxt &"], - [[], "This & is a &", "This & is a &"], - [['doubleEncode' => false], "This & is a &", "This & is a &"], - [['flags' => ENT_IGNORE], "With '\" quotes", "With '\" quotes"], - [[], "With '\" quotes", "With '" quotes"], - [['flags' => ENT_QUOTES], "With '\" quotes", "With '" quotes"], + [new HtmlEntitiesRule(), "This is some téxt &", "This is some téxt &"], + [new HtmlEntitiesRule(), "This & is a &", "This & is a &"], + [new HtmlEntitiesRule(doubleEncode: false), "This & is a &", "This & is a &"], + [new HtmlEntitiesRule(flags : ENT_IGNORE), "With '\" quotes", "With '\" quotes"], + [new HtmlEntitiesRule(), "With '\" quotes", "With '" quotes"], + [new HtmlEntitiesRule(flags :ENT_QUOTES), "With '\" quotes", "With '" quotes"], ]; } } diff --git a/tests/DMS/Filter/Filters/IntTest.php b/tests/DMS/Filter/Filters/IntTest.php index bf4851e..7f598e5 100644 --- a/tests/DMS/Filter/Filters/IntTest.php +++ b/tests/DMS/Filter/Filters/IntTest.php @@ -4,20 +4,13 @@ use DMS\Tests\FilterTestCase; use DMS\Filter\Rules\IntScalar as IntRule; +use PHPUnit\Framework\Attributes\DataProvider; class IntTest extends FilterTestCase { - - /** - * @dataProvider provideForRule - * - * @param $options - * @param $value - * @param $expectedResult - */ - public function testRule($options, $value, $expectedResult): void + #[DataProvider('provideForRule')] + public function testRule(IntRule $rule, $value, $expectedResult): void { - $rule = new IntRule($options); $filter = new IntScalar(); $result = $filter->apply($rule, $value); @@ -25,16 +18,16 @@ public function testRule($options, $value, $expectedResult): void $this->assertEquals($expectedResult, $result); } - public function provideForRule(): array + public static function provideForRule(): array { return [ - [null, "My Text", 0], - [null, true, 1], - [null, "21", 21], - [null, "21.2", 21], - [null, "21.9", 21], - [null, 21.9, 21], - [null, null, null], + [new IntRule(), "My Text", 0], + [new IntRule(), true, 1], + [new IntRule(), "21", 21], + [new IntRule(), "21.2", 21], + [new IntRule(), "21.9", 21], + [new IntRule(), 21.9, 21], + [new IntRule(), null, null], ]; } } diff --git a/tests/DMS/Filter/Filters/LaminasTest.php b/tests/DMS/Filter/Filters/LaminasTest.php index a4e5cc8..2568b99 100644 --- a/tests/DMS/Filter/Filters/LaminasTest.php +++ b/tests/DMS/Filter/Filters/LaminasTest.php @@ -1,5 +1,6 @@ buildRule('Boolean', ['casting' => false]); + $rule = $this->buildRule( + 'Laminas\Filter\DenyList', + ['list' => ['blocked@example.com', 'spam@example.com']] + ); + $filter = new Laminas(); - $filter->apply($rule, '0'); - $this->expectNotToPerformAssertions(); + $this->assertTrue(!is_null($filter->apply($rule, 'billy@example.com'))); + $this->assertTrue(is_null($filter->apply($rule, 'blocked@example.com'))); + $this->assertTrue(!is_null($filter->apply($rule, 'menphis@example.com'))); + $this->assertTrue(is_null($filter->apply($rule, 'spam@example.com'))); + $this->assertTrue(!is_null($filter->apply($rule, 'spam12@example.com'))); } - public function testFilterFullname(): void + public function testFilterBaseName(): void { - $rule = $this->buildRule('Laminas\Filter\Boolean', ['casting' => false]); + $rule = $this->buildRule('Laminas\Filter\BaseName'); $filter = new Laminas(); - $filter->apply($rule, '0'); - $this->expectNotToPerformAssertions(); + $this->assertSame('file.txt', $filter->apply($rule, '/path/to/file.txt')); } public function testInvalidFilter(): void @@ -36,11 +42,6 @@ public function testInvalidFilter(): void protected function buildRule($class, $options = []): LaminasRule { - return new LaminasRule( - [ - 'class' => $class, - 'laminasOptions' => $options, - ] - ); + return new LaminasRule($class, $options); } } diff --git a/tests/DMS/Filter/Filters/Loader/FilterLoaderTest.php b/tests/DMS/Filter/Filters/Loader/FilterLoaderTest.php index b7b3171..6d366e3 100644 --- a/tests/DMS/Filter/Filters/Loader/FilterLoaderTest.php +++ b/tests/DMS/Filter/Filters/Loader/FilterLoaderTest.php @@ -5,6 +5,7 @@ use DMS\Filter\Rules\StripTags; use DMS\Tests\Dummy\Rules\NoOptionsRule; use DMS\Tests\FilterTestCase; +use PHPUnit\Framework\Attributes\DataProvider; use UnexpectedValueException; class FilterLoaderTest extends FilterTestCase @@ -12,19 +13,13 @@ class FilterLoaderTest extends FilterTestCase protected FilterLoaderInterface $loader; public function setUp(): void -{ + { parent::setUp(); $this->loader = new FilterLoader(); } - /** - * @param $rule - * @param $return - * @param $expectException - * - * @dataProvider provideForGetFilter - */ + #[DataProvider('provideForGetFilter')] public function testGetFilterForRule($rule, $return, $expectException): void { if ($expectException) { @@ -34,7 +29,7 @@ public function testGetFilterForRule($rule, $return, $expectException): void $this->assertEquals($return, $this->loader->getFilterForRule($rule)); } - public function provideForGetFilter(): array + public static function provideForGetFilter(): array { return [ [new StripTags(), new \DMS\Filter\Filters\StripTags(), false], diff --git a/tests/DMS/Filter/Filters/PregReplaceTest.php b/tests/DMS/Filter/Filters/PregReplaceTest.php index 72a195c..260d2a8 100644 --- a/tests/DMS/Filter/Filters/PregReplaceTest.php +++ b/tests/DMS/Filter/Filters/PregReplaceTest.php @@ -4,20 +4,13 @@ use DMS\Tests\FilterTestCase; use DMS\Filter\Rules\PregReplace as PregReplaceRule; +use PHPUnit\Framework\Attributes\DataProvider; class PregReplaceTest extends FilterTestCase { - - /** - * @dataProvider provideForRule - * - * @param $options - * @param $value - * @param $expectedResult - */ - public function testRule($options, $value, $expectedResult): void + #[DataProvider('provideForRule')] + public function testRule(PregReplaceRule $rule, $value, $expectedResult): void { - $rule = new PregReplaceRule($options); $filter = new PregReplace(); $result = $filter->apply($rule, $value); @@ -25,14 +18,16 @@ public function testRule($options, $value, $expectedResult): void $this->assertEquals($expectedResult, $result); } - public function provideForRule(): array + public static function provideForRule(): array { return [ - [['regexp' => '/(old )/'], "the crazy old fox", "the crazy fox"], - [['regexp' => '/(old)/', 'replacement' => 'new'], "the crazy old fox", "the crazy new fox"], - [['regexp' => '/([0-9]*)/'], "this is day 21", "this is day "], - [['regexp' => '/(style=\"[^\"]*\")/'], "", "
"], - [['regexp' => '/(style=\"[^\"]*\")/'], null, null], + [new PregReplaceRule(regexp : '/(old )/'), "the crazy old fox", "the crazy fox"], + [new PregReplaceRule(regexp : '/(old)/', replacement : 'new'), "the crazy old fox", "the crazy new fox"], + [new PregReplaceRule(regexp : '/([0-9]*)/'), "this is day 21", "this is day "], + [new PregReplaceRule(regexp : '/(style=\"[^\"]*\")/'), + "
", "
" + ], + [new PregReplaceRule(regexp : '/(style=\"[^\"]*\")/'), null, null], ]; } } diff --git a/tests/DMS/Filter/Filters/StripNewlinesTest.php b/tests/DMS/Filter/Filters/StripNewlinesTest.php index caec3fa..c8ca664 100644 --- a/tests/DMS/Filter/Filters/StripNewlinesTest.php +++ b/tests/DMS/Filter/Filters/StripNewlinesTest.php @@ -4,20 +4,13 @@ use DMS\Tests\FilterTestCase; use DMS\Filter\Rules\StripNewlines as StripNewLinesRule; +use PHPUnit\Framework\Attributes\DataProvider; class StripNewlinesTest extends FilterTestCase { - - /** - * @dataProvider provideForRule - * - * @param $options - * @param $value - * @param $expectedResult - */ - public function testRule($options, $value, $expectedResult): void + #[DataProvider('provideForRule')] + public function testRule(StripNewLinesRule $rule, $value, $expectedResult): void { - $rule = new StripNewLinesRule($options); $filter = new StripNewlines(); $result = $filter->apply($rule, $value); @@ -25,17 +18,14 @@ public function testRule($options, $value, $expectedResult): void $this->assertEquals($expectedResult, $result); } - public function provideForRule(): array + public static function provideForRule(): array { return [ - [null, "My \n Text", "My Text"], - [null, "My \n\r Text", "My Text"], - [null, "My \r\n Text", "My Text"], - [ - null, "My -Text", "MyText" - ], - [null, null, null], + [new StripNewLinesRule(), "My \n Text", "My Text"], + [new StripNewLinesRule(), "My \n\r Text", "My Text"], + [new StripNewLinesRule(), "My \r\n Text", "My Text"], + [new StripNewLinesRule(), "MyText", "MyText"], + [new StripNewLinesRule(), null, null], ]; } } diff --git a/tests/DMS/Filter/Filters/StripTagsTest.php b/tests/DMS/Filter/Filters/StripTagsTest.php index 72a8966..bc79155 100644 --- a/tests/DMS/Filter/Filters/StripTagsTest.php +++ b/tests/DMS/Filter/Filters/StripTagsTest.php @@ -4,20 +4,13 @@ use DMS\Tests\FilterTestCase; use DMS\Filter\Rules\StripTags as StripTagsRule; +use PHPUnit\Framework\Attributes\DataProvider; class StripTagsTest extends FilterTestCase { - - /** - * @dataProvider provideForRule - * - * @param $options - * @param $value - * @param $expectedResult - */ - public function testRule($options, $value, $expectedResult): void + #[DataProvider('provideForRule')] + public function testRule(StripTagsRule $rule, $value, $expectedResult): void { - $rule = new StripTagsRule($options); $filter = new StripTags(); $result = $filter->apply($rule, $value); @@ -25,15 +18,15 @@ public function testRule($options, $value, $expectedResult): void $this->assertEquals($expectedResult, $result); } - public function provideForRule(): array + public static function provideForRule(): array { return [ - [[], "my text", "my text"], - [[], "my < not an html tag> text", "my < not an html tag> text"], - [[], "in this case a < 2 a > 3;", "in this case a < 2 a > 3;"], - [['allowed' => "

"], "

my text

", "

my text

"], - ["

", "

my text

", "

my text

"], - [[], null, null], + [new StripTagsRule(), "my text", "my text"], + [new StripTagsRule(), "my < not an html tag> text", "my < not an html tag> text"], + [new StripTagsRule(), "in this case a < 2 a > 3;", "in this case a < 2 a > 3;"], + [new StripTagsRule(allowed : "

"), "

my text

", "

my text

"], + [new StripTagsRule(allowed: ""), "

my text

", "my text"], + [new StripTagsRule(), null, null], ]; } } diff --git a/tests/DMS/Filter/Filters/ToLowerTest.php b/tests/DMS/Filter/Filters/ToLowerTest.php index 47ff611..98a6d18 100644 --- a/tests/DMS/Filter/Filters/ToLowerTest.php +++ b/tests/DMS/Filter/Filters/ToLowerTest.php @@ -5,25 +5,17 @@ use DMS\Filter\Exception\FilterException; use DMS\Tests\FilterTestCase; use DMS\Filter\Rules\ToLower as ToLowerRule; +use PHPUnit\Framework\Attributes\DataProvider; class ToLowerTest extends FilterTestCase { - - /** - * @dataProvider provideForRule - * - * @param $options - * @param $value - * @param $expectedResult - * @param $useEncoding - */ - public function testRule($options, $value, $expectedResult, $useEncoding): void + #[DataProvider('provideForRule')] + public function testRule(ToLowerRule $rule, $value, $expectedResult, $useEncoding): void { if ($useEncoding && !function_exists('mb_strtolower')) { $this->markTestSkipped('mbstring extension not enabled'); } - $rule = new ToLowerRule($options); $filter = new ToLower(); $result = $filter->apply($rule, $value); @@ -38,22 +30,22 @@ public function testInvalidEncoding(): void $this->markTestSkipped('mbstring extension not enabled'); } - $rule = new ToLowerRule(['encoding' => 'invalid']); + $rule = new ToLowerRule(encoding : 'invalid'); $filter = new ToLower(); - $result = $filter->apply($rule, 'x'); + $filter->apply($rule, 'x'); } - public function provideForRule(): array + public static function provideForRule(): array { return [ - [['encoding' => 'utf-8'], "MY TEXT", "my text", true], - [['encoding' => 'utf-8'], "MY Ã TEXT", "my ã text", true], - [['encoding' => 'utf-8'], "MY Á TEXT", "my á text", true], - ['utf-8', "MY Á TEXT", "my á text", true], - [[], "MY TEXT", "my text", false], - [[], "MY TEXT", "my text", false], - [[], null, null, false], + [new ToLowerRule(encoding: 'utf-8'), "MY TEXT", "my text", true], + [new ToLowerRule(encoding: 'utf-8'), "MY Ã TEXT", "my ã text", true], + [new ToLowerRule(encoding: 'utf-8'), "MY Á TEXT", "my á text", true], + [new ToLowerRule(encoding: 'utf-8'), "MY À TEXT", "my à text", true], + [new ToLowerRule(), "MY TEXT", "my text", false], + [new ToLowerRule(), "MY TEXT", "my text", false], + [new ToLowerRule(), null, null, false], ]; } } diff --git a/tests/DMS/Filter/Filters/ToUpperTest.php b/tests/DMS/Filter/Filters/ToUpperTest.php index 63766c6..ab33f78 100644 --- a/tests/DMS/Filter/Filters/ToUpperTest.php +++ b/tests/DMS/Filter/Filters/ToUpperTest.php @@ -5,25 +5,17 @@ use DMS\Filter\Exception\FilterException; use DMS\Tests\FilterTestCase; use DMS\Filter\Rules\ToUpper as ToUpperRule; +use PHPUnit\Framework\Attributes\DataProvider; class ToUpperTest extends FilterTestCase { - - /** - * @dataProvider provideForRule - * - * @param $options - * @param $value - * @param $expectedResult - * @param $useEncoding - */ - public function testRule($options, $value, $expectedResult, $useEncoding): void + #[DataProvider('provideForRule')] + public function testRule(ToUpperRule $rule, $value, $expectedResult, $useEncoding): void { if ($useEncoding && !function_exists('mb_strtoupper')) { $this->markTestSkipped('mbstring extension not enabled'); } - $rule = new ToUpperRule($options); $filter = new ToUpper(); $result = $filter->apply($rule, $value); @@ -38,22 +30,22 @@ public function testInvalidEncoding(): void $this->markTestSkipped('mbstring extension not enabled'); } - $rule = new ToUpperRule(['encoding' => 'invalid']); + $rule = new ToUpperRule(encoding :'invalid'); $filter = new ToUpper(); - $result = $filter->apply($rule, 'x'); + $r = $filter->apply($rule, 'x'); } - public function provideForRule(): array + public static function provideForRule(): array { return [ - [['encoding' => 'utf-8'], "my text", "MY TEXT", true], - [['encoding' => 'utf-8'], "my ã text", "MY Ã TEXT", true], - [['encoding' => 'utf-8'], "my á text", "MY Á TEXT", true], - ['utf-8', "my á text", "MY Á TEXT", true], - [[], "my text", "MY TEXT", false], - [[], "my text", "MY TEXT", false], - [[], null, null, false], + [new ToUpperRule(encoding: 'utf-8'), "my text", "MY TEXT", true], + [new ToUpperRule(encoding: 'utf-8'), "my ã text", "MY Ã TEXT", true], + [new ToUpperRule(encoding: 'utf-8'), "my á text", "MY Á TEXT", true], + [new ToUpperRule(encoding: 'utf-8'), "my à text", "MY À TEXT", true], + [new ToUpperRule(), "my text", "MY TEXT", false], + [new ToUpperRule(), "my text", "MY TEXT", false], + [new ToUpperRule(), null, null, false], ]; } } diff --git a/tests/DMS/Filter/Filters/TrimTest.php b/tests/DMS/Filter/Filters/TrimTest.php index d790227..9c933f1 100644 --- a/tests/DMS/Filter/Filters/TrimTest.php +++ b/tests/DMS/Filter/Filters/TrimTest.php @@ -4,20 +4,13 @@ use DMS\Tests\FilterTestCase; use DMS\Filter\Rules\Trim as TrimRule; +use PHPUnit\Framework\Attributes\DataProvider; class TrimTest extends FilterTestCase { - - /** - * @dataProvider provideForRule - * - * @param $options - * @param $value - * @param $expectedResult - */ - public function testRule($options, $value, $expectedResult): void + #[DataProvider('provideForRule')] + public function testRule(TrimRule $rule, $value, $expectedResult): void { - $rule = new TrimRule($options); $filter = new Trim(); $result = $filter->apply($rule, $value); @@ -25,16 +18,16 @@ public function testRule($options, $value, $expectedResult): void $this->assertEquals($expectedResult, $result); } - public function provideForRule(): array + public static function provideForRule(): array { return [ - [[], " my text", "my text"], - [[], " my text ", "my text"], - [[], "my text ", "my text"], - [['charlist' => "\\"], "\my text", "my text"], - ["\\", "\my text", "my text"], - ["x", "xmy textx", "my text"], - [[], null, null], + [new TrimRule(), " my text", "my text"], + [new TrimRule(), " my text ", "my text"], + [new TrimRule(), "my text ", "my text"], + [new TrimRule(charlist: '\\'), "\my text", "my text"], + [new TrimRule(charlist: "#"), "#my text##", "my text"], + [new TrimRule(charlist: 'x'), "xmy textx", "my text"], + [new TrimRule(), null, null], ]; } } diff --git a/tests/DMS/Filter/Mapping/ClassMetadataFactoryTest.php b/tests/DMS/Filter/Mapping/ClassMetadataFactoryTest.php index 60d316e..3031577 100644 --- a/tests/DMS/Filter/Mapping/ClassMetadataFactoryTest.php +++ b/tests/DMS/Filter/Mapping/ClassMetadataFactoryTest.php @@ -2,21 +2,18 @@ namespace DMS\Filter\Mapping; -use DMS\Filter\Mapping\Loader\AnnotationLoader; use DMS\Filter\Mapping\Loader\AttributeLoader; use DMS\Filter\Mapping\Loader\LoaderInterface; use DMS\Tests\Dummy\Classes\AttributedClass; use DMS\Tests\FilterTestCase; -use Doctrine\Common\Annotations\AnnotationReader; -use Doctrine\Common\Cache\ArrayCache; -use DMS\Tests\Dummy\Classes\AnnotatedClass; use Generator; +use PHPUnit\Framework\Attributes\DataProvider; +use Psr\Cache\InvalidArgumentException; +use Symfony\Component\Cache\Adapter\ArrayAdapter; class ClassMetadataFactoryTest extends FilterTestCase { - /** - * @dataProvider factoryDataProvider - */ + #[DataProvider('factoryDataProvider')] public function testGetClassMetadata(ClassMetadataFactory $factory, $class): void { $metadata = $factory->getClassMetadata($class); @@ -25,8 +22,9 @@ public function testGetClassMetadata(ClassMetadataFactory $factory, $class): voi } /** - * @dataProvider factoryDataProvider + * @throws InvalidArgumentException */ + #[DataProvider('factoryDataProvider')] public function testParsedMetadataFromFactory(ClassMetadataFactory $factory, $class): void { $metadata = $factory->getClassMetadata($class); @@ -37,16 +35,17 @@ public function testParsedMetadataFromFactory(ClassMetadataFactory $factory, $cl } /** - * @dataProvider loaderDataProvider + * @throws InvalidArgumentException */ + #[DataProvider('loaderDataProvider')] public function testCachedMetadataFromFactory(LoaderInterface $loader, $class): void { - $cache = new ArrayCache(); + $cache = new ArrayAdapter(); $factory = new ClassMetadataFactory($loader, $cache); $metadata = $factory->getClassMetadata($class); - $this->assertTrue($cache->contains(ltrim($class, '\\'))); + $this->assertTrue($cache->getItem(ltrim($class, '\\'))->isHit()); //Get new Factory to retrieve from cache $factory = new ClassMetadataFactory($loader, $cache); @@ -55,26 +54,16 @@ public function testCachedMetadataFromFactory(LoaderInterface $loader, $class): $this->assertEquals($metadata, $metadataCached); } - public function factoryDataProvider(): Generator + public static function factoryDataProvider(): Generator { - yield 'Annotation' => [ - $this->buildMetadataFactoryWithAnnotationLoader(), - AnnotatedClass::class, - ]; - yield 'Attribute' => [ - $this->buildMetadataFactoryWithAttributeLoader(), + self::buildMetadataFactoryWithAttributeLoader(), AttributedClass::class, ]; } - - public function loaderDataProvider(): Generator - { - yield 'Annotation' => [ - new AnnotationLoader(new AnnotationReader()), - AnnotatedClass::class, - ]; + public static function loaderDataProvider(): Generator + { yield 'Attribute' => [ new AttributeLoader(), AttributedClass::class, diff --git a/tests/DMS/Filter/Mapping/Loader/AttributeLoaderTest.php b/tests/DMS/Filter/Mapping/Loader/AttributeLoaderTest.php index aa84277..79345bb 100644 --- a/tests/DMS/Filter/Mapping/Loader/AttributeLoaderTest.php +++ b/tests/DMS/Filter/Mapping/Loader/AttributeLoaderTest.php @@ -1,30 +1,30 @@ -loadClassMetadata($metadata); - - $this->assertTrue($loadMetadataResult); - - $classProperties = array_map( - fn (ReflectionProperty $property) => $property->getName(), - $metadata->getReflectionClass()->getProperties() - ); - - $this->assertSame($classProperties, $metadata->getFilteredProperties()); - } -} +loadClassMetadata($metadata); + + $this->assertTrue($loadMetadataResult); + + $classProperties = array_map( + fn (ReflectionProperty $property) => $property->getName(), + $metadata->getReflectionClass()->getProperties() + ); + + $this->assertSame($classProperties, $metadata->getFilteredProperties()); + } +} diff --git a/tests/DMS/Filter/Rules/CallbackTest.php b/tests/DMS/Filter/Rules/CallbackTest.php index 7fcc80c..1f6b7a3 100644 --- a/tests/DMS/Filter/Rules/CallbackTest.php +++ b/tests/DMS/Filter/Rules/CallbackTest.php @@ -1,24 +1,16 @@ assertEquals($expectedOutput, $rule->getInputType()); } - public function provideInputs(): array + public static function provideInputs(): array { - $closure = static function ($v) {}; + $closure = static function ($v) { + }; return [ ['objMethod', Callback::SELF_METHOD_TYPE, false], - [[AnnotatedClass::class, 'anotherCallback'], Callback::CALLABLE_TYPE, false], - [[AnnotatedClass::class, 'missingCallback'], null, true], - [[new AnnotatedClass(), 'callbackMethod'], Callback::CALLABLE_TYPE, false], [[AttributedClass::class, 'anotherCallback'], Callback::CALLABLE_TYPE, false], [[AttributedClass::class, 'missingCallback'], null, true], [[new AttributedClass(), 'callbackMethod'], Callback::CALLABLE_TYPE, false], diff --git a/tests/DMS/Filter/Rules/RuleTest.php b/tests/DMS/Filter/Rules/RuleTest.php index e85b57e..3c3b4f8 100644 --- a/tests/DMS/Filter/Rules/RuleTest.php +++ b/tests/DMS/Filter/Rules/RuleTest.php @@ -2,20 +2,13 @@ namespace DMS\Filter\Rules; -use DMS\Filter\Exception\InvalidOptionsException; -use DMS\Filter\Exception\MissingOptionsException; -use DMS\Filter\Exception\RuleDefinitionException; use DMS\Tests\Dummy\Rules\DefaultOptionRule; -use DMS\Tests\Dummy\Rules\InvalidDefaultOptionRule; -use DMS\Tests\Dummy\Rules\MultipleOptionsRule; use DMS\Tests\Dummy\Rules\NoOptionsRule; use DMS\Tests\Dummy\Rules\RequiredOptionsRule; use DMS\Tests\FilterTestCase; -use DMS\Filter\Rules\Rule; class RuleTest extends FilterTestCase { - public function testConstructorHappyPath(): void { $rule = new NoOptionsRule(); @@ -43,41 +36,4 @@ public function testConstructorHappyPathWithRequired(): void $this->assertInstanceOf(Rule::class, $rule); } - - public function testConstructorNoDefinedDefaultOption(): void - { - $this->expectException(RuleDefinitionException::class); - $rule = new NoOptionsRule('value'); - - $this->assertInstanceOf(Rule::class, $rule); - } - - public function testConstructorInvalidOption(): void - { - $this->expectException(InvalidOptionsException::class); - $rule = new MultipleOptionsRule(['invalid' => 'option']); - } - - public function testConstructorInvalidDefaultOption(): void - { - $this->expectException(InvalidOptionsException::class); - $rule = new InvalidDefaultOptionRule('value'); - } - - public function testConstructorMissingOption(): void - { - $this->expectException(MissingOptionsException::class); - $rule = new RequiredOptionsRule(['config' => 'option']); - } - - public function testOptionExceptionInformation(): void - { - try { - $rule = new MultipleOptionsRule(['invalid' => 'option']); - } catch (InvalidOptionsException $e) { - $this->assertIsArray($e->getOptions()); - - $this->assertContains('invalid', $e->getOptions()); - } - } } diff --git a/tests/DMS/Tests/Dummy/Classes/AttributedClass.php b/tests/DMS/Tests/Dummy/Classes/AttributedClass.php index 4600361..7bb60cc 100644 --- a/tests/DMS/Tests/Dummy/Classes/AttributedClass.php +++ b/tests/DMS/Tests/Dummy/Classes/AttributedClass.php @@ -17,16 +17,16 @@ class AttributedClass #[Filter\Callback("callbackMethod")] public ?string $callback = null; - + #[Filter\Callback(["DMS\Tests\Dummy\Classes\AnnotatedClass", "anotherCallback"])] public ?string $callback2 = null; - public function callbackMethod($value): string + public function callbackMethod(): string { return 'called_back'; } - - public static function anotherCallback($value): string + + public static function anotherCallback(): string { return 'called_back'; } diff --git a/tests/DMS/Tests/Dummy/Rules/MultipleOptionsRule.php b/tests/DMS/Tests/Dummy/Rules/MultipleOptionsRule.php index bc873f9..4ea9f82 100644 --- a/tests/DMS/Tests/Dummy/Rules/MultipleOptionsRule.php +++ b/tests/DMS/Tests/Dummy/Rules/MultipleOptionsRule.php @@ -6,7 +6,6 @@ class MultipleOptionsRule extends Rule { - /** * @var mixed */ diff --git a/tests/DMS/Tests/FilterTestCase.php b/tests/DMS/Tests/FilterTestCase.php index 5423632..4494803 100644 --- a/tests/DMS/Tests/FilterTestCase.php +++ b/tests/DMS/Tests/FilterTestCase.php @@ -4,7 +4,6 @@ use DMS\Filter\Mapping; use DMS\Filter\Mapping\ClassMetadataFactory; -use Doctrine\Common\Annotations; use PHPUnit\Framework\TestCase; class FilterTestCase extends TestCase @@ -19,16 +18,7 @@ public function tearDown(): void parent::tearDown(); } - protected function buildMetadataFactoryWithAnnotationLoader(): ClassMetadataFactory - { - $reader = new Annotations\AnnotationReader(); - - $loader = new Mapping\Loader\AnnotationLoader($reader); - - return new ClassMetadataFactory($loader); - } - - protected function buildMetadataFactoryWithAttributeLoader(): ClassMetadataFactory + protected static function buildMetadataFactoryWithAttributeLoader(): ClassMetadataFactory { $loader = new Mapping\Loader\AttributeLoader(); From f715a53c937dca9aa63543d749721a6173cec09c Mon Sep 17 00:00:00 2001 From: Igor Kusmitsch Date: Wed, 25 Sep 2024 16:25:20 -0300 Subject: [PATCH 11/15] Update changelog --- CHANGELOG.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e032ca4..06c4b2f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,21 @@ # Changelog +## [6.0.0] + +Changed +- [BC Break] Doctrine Annotations are no longer used in this project. Annotations have been replaced by PHP attributes. If your codebase or any third-party dependencies still rely on annotations, please migrate to attributes or adjust accordingly. +- [BC Break] The project now requires PHP 8.2 or higher. Ensure that your development and production environments are updated to PHP 8.2+. +- [BC Break] The project now requires PHPUnit version 11 or higher. Ensure that your environment is compatible with PHPUnit 11+ before running tests. +- [BC Break] All classes previously marked with @deprecated and replaced by {@link Laminas} have been removed. If your code still relies on these deprecated classes, please update your references to use the corresponding Laminas classes. + +- Add squizlabs/php_codesniffer library. +- Add require-dev ext-pcov for PHPUnit Code Coverage +- PHPCS Coding Standard: The project now exclusively uses PSR-12 as the coding standard. This change ensures that the code adheres to the PSR-12 guidelines, promoting consistency and readability. +- Updated the laminas/laminas-filter bundle from version ^2.9 to ^2.37 +- Remove the laminas/laminas-zendframework-bridge bundle. +- Remove the dms/coding-standard bundle. + + From v3.0.0 onwards this file will always be updated with notable changes and BC breaks. This project follows semver. From 3b7a8a8659cc9074ee5b5dc1edb612cd33fa8ef4 Mon Sep 17 00:00:00 2001 From: Igor Kusmitsch Date: Wed, 25 Sep 2024 16:25:40 -0300 Subject: [PATCH 12/15] Update readme file --- README.md | 90 ++++--------------------------------------------------- 1 file changed, 6 insertions(+), 84 deletions(-) diff --git a/README.md b/README.md index d55d204..1735112 100644 --- a/README.md +++ b/README.md @@ -12,79 +12,6 @@ Use composer to add DMS\Filter to your app ## Usage -### Annotation way - -Your Entity: - -```php - -``` - -Filtering: - -```php -setEnableParsePhpImports(true); - - //Load AnnotationLoader - $loader = new Mapping\Loader\AnnotationLoader($reader); - $this->loader = $loader; - - //Get a MetadataFactory - $metadataFactory = new Mapping\ClassMetadataFactory($loader); - - //Get a Filter - $filter = new DMS\Filter\Filter($metadataFactory); - - - //Get your Entity - $user = new App\Entity\User(); - $user->name = "My name"; - $user->email = " email@mail.com"; - - //Filter you entity - $filter->filter($user); - - echo $user->name; //"My name" - echo $user->email; //"email@mail.com" -?> -``` - -Full example: https://gist.github.com/1098352 - -### Attribute way - Your Entity: ```php @@ -115,13 +42,15 @@ Filtering: loader = $loader; //Get a MetadataFactory $metadataFactory = new Mapping\ClassMetadataFactory($loader); + //Get a FilterLoader + $filterLoader = new \DMS\Filter\Filters\Loader\FilterLoader(); + //Get a Filter - $filter = new DMS\Filter\Filter($metadataFactory); + $filter = new DMS\Filter\Filter($metadataFactory, $filterLoader); //Get your Entity @@ -130,19 +59,12 @@ Filtering: $user->email = " email@mail.com"; //Filter you entity - $filter->filter($user); + $filter->filterEntity($user); echo $user->name; //"My name" echo $user->email; //"email@mail.com" ?> ``` - -## Dependencies - -This package relies on these external libraries: - -* Doctrine Annotations - ## Contributing Feel free to send pull requests, just follow these guides: @@ -157,4 +79,4 @@ Feel free to send pull requests, just follow these guides: This library is inspired by the Symfony 2 Validator component and is meant to work alongside it. -Symfony 2 Validator: https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Validator +Symfony Validator: https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Validator From 6d54dc67af5a123ff5e038ad32b5429b329561a1 Mon Sep 17 00:00:00 2001 From: Igor Kusmitsch Date: Wed, 25 Sep 2024 16:27:44 -0300 Subject: [PATCH 13/15] Update Workflows --- .github/workflows/check-cs.yml | 13 +++++++++---- .github/workflows/run-tests.yml | 10 +++------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/.github/workflows/check-cs.yml b/.github/workflows/check-cs.yml index c663758..343d063 100644 --- a/.github/workflows/check-cs.yml +++ b/.github/workflows/check-cs.yml @@ -25,16 +25,21 @@ jobs: - name: Setup PHP uses: shivammathur/setup-php@v2 with: - php-version: 8.1 - coverage: none + php-version: 8.2 + coverage: pcov tools: composer, cs2pr + extensions: pcov - name: Install dependencies run: | - composer install --prefer-dist --no-suggest --no-progress + composer install --prefer-dist --no-progress + + - name: Check + run: | + ls -la vendor/bin - name: Check Code Style - run: vendor/bin/phpcs --report-full --report-checkstyle=./phpcs-report.xml + run: ./vendor/bin/phpcs --report-full --report-checkstyle=./phpcs-report.xml - name: Show PHPCS results in PR run: cs2pr ./phpcs-report.xml diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 52ae7b8..d1b4f4c 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -17,15 +17,10 @@ jobs: strategy: matrix: - php: [8.0, 8.1, 8.2] + php: [8.2, 8.3] dependency-version: [--prefer-lowest, --prefer-stable] experimental: [false] - include: - - php: '8.3' - dependency-version: --prefer-stable - experimental: true - name: P${{ matrix.php }} - ${{ matrix.dependency-version }} steps: @@ -36,8 +31,9 @@ jobs: uses: shivammathur/setup-php@v2 with: php-version: ${{ matrix.php }} - coverage: none + coverage: pcov tools: composer + extensions: pcov - name: Install dependencies run: | From 4b81ca27a320d37d6f2c2dd69a65600658f4c810 Mon Sep 17 00:00:00 2001 From: Igor Kusmitsch Date: Wed, 25 Sep 2024 16:38:59 -0300 Subject: [PATCH 14/15] Adjust PHPCS configuration --- phpcs.xml.dist | 1 - 1 file changed, 1 deletion(-) diff --git a/phpcs.xml.dist b/phpcs.xml.dist index 06d9a96..dc0696f 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -3,7 +3,6 @@ src - tests vendor/* \ No newline at end of file From e9e1e90ddbecbe2875d366f01c15493a6e73fe21 Mon Sep 17 00:00:00 2001 From: Igor Kusmitsch Date: Wed, 25 Sep 2024 16:49:27 -0300 Subject: [PATCH 15/15] remove unnecessary check in workflow --- .github/workflows/check-cs.yml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/.github/workflows/check-cs.yml b/.github/workflows/check-cs.yml index 343d063..2db77f3 100644 --- a/.github/workflows/check-cs.yml +++ b/.github/workflows/check-cs.yml @@ -34,10 +34,6 @@ jobs: run: | composer install --prefer-dist --no-progress - - name: Check - run: | - ls -la vendor/bin - - name: Check Code Style run: ./vendor/bin/phpcs --report-full --report-checkstyle=./phpcs-report.xml