Skip to content

Commit

Permalink
Merge pull request #52 from rdohms/kdadler-php8-nullable-fields
Browse files Browse the repository at this point in the history
PHP 8 fixes
  • Loading branch information
rdohms authored Oct 11, 2021
2 parents ef4c214 + cf42bdd commit 2ca1a5f
Show file tree
Hide file tree
Showing 16 changed files with 176 additions and 32 deletions.
45 changes: 18 additions & 27 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,30 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- https://phpunit.de/manual/current/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
backupGlobals="false"
colors="true"
verbose="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTestsThatDoNotTestAnything="true"
beStrictAboutTodoAnnotatedTests="true"
beStrictAboutChangesToGlobalState="true"
>
<testsuites>
<testsuite name="DMS Filter Suite">
<directory>tests/DMS</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory suffix=".php">src/DMS</directory>
</whitelist>
</filter>

<logging>
<log type="coverage-html" target="tests/_reports/coverage/" lowUpperBound="35" highLowerBound="70"/>
<log type="testdox-text" target="tests/_reports/testdox/tests.txt"/>
<log type="testdox-html" target="tests/_reports/testdox/tests.html"/>
</logging>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" backupGlobals="false" colors="true" verbose="true" beStrictAboutOutputDuringTests="true" beStrictAboutTestsThatDoNotTestAnything="true" beStrictAboutTodoAnnotatedTests="true" beStrictAboutChangesToGlobalState="true">
<coverage>
<include>
<directory suffix=".php">src/DMS</directory>
</include>
<report>
<html outputDirectory="tests/_reports/coverage/" lowUpperBound="35" highLowerBound="70"/>
</report>
</coverage>
<testsuites>
<testsuite name="DMS Filter Suite">
<directory>tests/DMS</directory>
</testsuite>
</testsuites>
<logging>
<testdoxText outputFile="tests/_reports/testdox/tests.txt"/>
<testdoxHtml outputFile="tests/_reports/testdox/tests.html"/>
</logging>
</phpunit>
64 changes: 64 additions & 0 deletions src/DMS/Filter/Filters/Laminas.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
declare(strict_types=1);

namespace DMS\Filter\Filters;

use DMS\Filter\Exception\InvalidZendFilterException;
use DMS\Filter\FilterInterface;
use DMS\Filter\Rules\Laminas as LaminasRule;
use DMS\Filter\Rules\Rule;
use ReflectionException;
use ReflectionMethod;

use function class_exists;
use function sprintf;
use function strpos;

/**
* Laminas Filter
*
* Instantiates and runs Laminas Filters (from ZF2)
*/
class Laminas extends BaseFilter
{
/**
* {@inheritDoc}
*
* @param LaminasRule $rule
*/
public function apply(Rule $rule, $value)
{
return $this->getLaminasInstance($rule->class, $rule->laminasOptions)->filter($value);
}

/**
* Instantiates a configured Laminas Filter, if it exists
*
* @param mixed[] $options
*
* @return FilterInterface|object
*
* @throws InvalidZendFilterException
*/
public function getLaminasInstance(string $class, array $options): object
{
if (strpos($class, 'Laminas\Filter') === false) {
$class = 'Laminas\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 $e) {
return new $class($options);
}
}
}
3 changes: 2 additions & 1 deletion src/DMS/Filter/Filters/PregReplace.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use DMS\Filter\Rules\Rule;

use function is_string;
use function preg_replace;

/**
Expand All @@ -20,6 +21,6 @@ class PregReplace extends BaseFilter
*/
public function apply(Rule $rule, $value)
{
return preg_replace($rule->regexp, $rule->replacement, $value);
return is_string($value) ? preg_replace($rule->regexp, $rule->replacement, $value) : $value;
}
}
5 changes: 3 additions & 2 deletions src/DMS/Filter/Filters/RegExp.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use DMS\Filter\Rules\Rule;

use function is_string;
use function preg_match;
use function preg_replace;

Expand Down Expand Up @@ -32,15 +33,15 @@ public function apply(Rule $rule, $value)
? $rule->unicodePattern
: $rule->pattern;

return preg_replace($pattern, '', $value);
return is_string($value) ? preg_replace($pattern, '', $value) : $value;
}

/**
* Verifies that Regular Expression functions support unicode
*/
public function checkUnicodeSupport(): bool
{
if (static::$unicodeEnabled === null) {
if (! isset(static::$unicodeEnabled)) {
//phpcs:disable SlevomatCodingStandard.ControlStructures.UselessTernaryOperator.UselessTernaryOperator
static::$unicodeEnabled = @preg_match('/\pL/u', 'a') ? true : false;
}
Expand Down
3 changes: 2 additions & 1 deletion src/DMS/Filter/Filters/StripNewlines.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use DMS\Filter\Rules\Rule;

use function is_string;
use function str_replace;

/**
Expand All @@ -17,6 +18,6 @@ class StripNewlines extends BaseFilter
*/
public function apply(Rule $rule, $value)
{
return str_replace(["\n", "\r"], '', $value);
return is_string($value) ? str_replace(["\n", "\r"], '', $value) : $value;
}
}
3 changes: 2 additions & 1 deletion src/DMS/Filter/Filters/StripTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use DMS\Filter\Rules\Rule;

use function is_string;
use function strip_tags;

/**
Expand All @@ -19,6 +20,6 @@ class StripTags extends BaseFilter
*/
public function apply(Rule $rule, $value)
{
return strip_tags($value, $rule->allowed);
return is_string($value) ? strip_tags($value, $rule->allowed) : $value;
}
}
31 changes: 31 additions & 0 deletions src/DMS/Filter/Rules/Laminas.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
declare(strict_types=1);

namespace DMS\Filter\Rules;

/**
* Laminas Rule
*
* Allows the use for Laminas Filters
*
* @Annotation
*/
class Laminas extends Rule
{
/**
* Laminas\Filter class, can be either a FQN or just Boolean for example
*/
public string $class;

/**
* Array of options to be passed into the Laminas Filter
*
* @var mixed[]
*/
public array $laminasOptions = [];

public function getDefaultOption(): ?string
{
return 'class';
}
}
1 change: 1 addition & 0 deletions tests/DMS/Filter/Filters/AlnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public function provideForRule(): array
[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],
];
}
}
1 change: 1 addition & 0 deletions tests/DMS/Filter/Filters/AlphaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public function provideForRule(): array
[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],
];
}
}
1 change: 1 addition & 0 deletions tests/DMS/Filter/Filters/IntTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function provideForRule(): array
[null, "21.2", 21],
[null, "21.9", 21],
[null, 21.9, 21],
[null, null, null],
];
}
}
46 changes: 46 additions & 0 deletions tests/DMS/Filter/Filters/LaminasTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php


namespace DMS\Filter\Filters;

use DMS\Filter\Exception\InvalidZendFilterException;
use DMS\Filter\Rules\Laminas as LaminasRule;
use DMS\Tests\FilterTestCase;

class LaminasTest extends FilterTestCase
{

public function testFilterShortname(): void
{
$rule = $this->buildRule('Boolean', ['casting' => false]);
$filter = new Laminas();
$filter->apply($rule, '0');
$this->expectNotToPerformAssertions();
}

public function testFilterFullname(): void
{
$rule = $this->buildRule('Laminas\Filter\Boolean', ['casting' => false]);
$filter = new Laminas();
$filter->apply($rule, '0');
$this->expectNotToPerformAssertions();
}

public function testInvalidFilter(): void
{
$this->expectException(InvalidZendFilterException::class);
$rule = $this->buildRule('MissingFilter');
$filter = new Laminas();
$filter->apply($rule, '0');
}

protected function buildRule($class, $options = []): LaminasRule
{
return new LaminasRule(
[
'class' => $class,
'laminasOptions' => $options,
]
);
}
}
1 change: 1 addition & 0 deletions tests/DMS/Filter/Filters/PregReplaceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function provideForRule(): array
[['regexp' => '/(old)/', 'replacement' => 'new'], "the crazy old fox", "the crazy new fox"],
[['regexp' => '/([0-9]*)/'], "this is day 21", "this is day "],
[['regexp' => '/(style=\"[^\"]*\")/'], "<table style=\"width: 23px\" class=\"myclass\">", "<table class=\"myclass\">"],
[['regexp' => '/(style=\"[^\"]*\")/'], null, null],
];
}
}
1 change: 1 addition & 0 deletions tests/DMS/Filter/Filters/StripNewlinesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public function provideForRule(): array
null, "My
Text", "MyText"
],
[null, null, null],
];
}
}
1 change: 1 addition & 0 deletions tests/DMS/Filter/Filters/StripTagsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function provideForRule(): array
[[], "<b>in this case a < 2 a > 3;</b>", "in this case a < 2 a > 3;"],
[['allowed' => "<p>"], "<b><p>my text</p></b>", "<p>my text</p>"],
["<p>", "<b><p>my text</p></b>", "<p>my text</p>"],
[[], null, null],
];
}
}
1 change: 1 addition & 0 deletions tests/DMS/Filter/Filters/ToLowerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public function provideForRule(): array
['utf-8', "MY Á TEXT", "my á text", true],
[[], "MY TEXT", "my text", false],
[[], "MY TEXT", "my text", false],
[[], null, null, false],
];
}
}
1 change: 1 addition & 0 deletions tests/DMS/Filter/Filters/ToUpperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public function provideForRule(): array
['utf-8', "my á text", "MY Á TEXT", true],
[[], "my text", "MY TEXT", false],
[[], "my text", "MY TEXT", false],
[[], null, null, false],
];
}
}

0 comments on commit 2ca1a5f

Please sign in to comment.