Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHP8.2, Attributes and deprecate doctrine #98

Merged
merged 15 commits into from
Sep 25, 2024
9 changes: 5 additions & 4 deletions .github/workflows/check-cs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,17 @@ 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 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
10 changes: 3 additions & 7 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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: |
Expand Down
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.

Expand Down
90 changes: 6 additions & 84 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,79 +12,6 @@ Use composer to add DMS\Filter to your app

## Usage

### Annotation way

Your Entity:

```php
<?php

namespace App\Entity;

//Import Annotations
use DMS\Filter\Rules as Filter;

class User
{

/**
* @Filter\StripTags()
* @Filter\Trim()
* @Filter\StripNewlines()
*
* @var string
*/
public string $name;

/**
* @Filter\StripTags()
* @Filter\Trim()
* @Filter\StripNewlines()
*
* @var string
*/
public string $email;

}
?>
```

Filtering:

```php
<?php
//Get Doctrine Reader
$reader = new Annotations\AnnotationReader();
$reader->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 <b>name</b>";
$user->email = " [email protected]";

//Filter you entity
$filter->filter($user);

echo $user->name; //"My name"
echo $user->email; //"[email protected]"
?>
```

Full example: https://gist.github.com/1098352

### Attribute way

Your Entity:

```php
Expand Down Expand Up @@ -115,13 +42,15 @@ Filtering:
<?php
//Load AttributeLoader
$loader = new Mapping\Loader\AttributeLoader();
$this->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
Expand All @@ -130,19 +59,12 @@ Filtering:
$user->email = " [email protected]";

//Filter you entity
$filter->filter($user);
$filter->filterEntity($user);

echo $user->name; //"My name"
echo $user->email; //"[email protected]"
?>
```

## Dependencies

This package relies on these external libraries:

* Doctrine Annotations

## Contributing

Feel free to send pull requests, just follow these guides:
Expand All @@ -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
13 changes: 6 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
18 changes: 3 additions & 15 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -1,20 +1,8 @@
<?xml version="1.0"?>
<ruleset name="PHPCS Coding Standards for DMS">

<arg name="basepath" value="."/>
<arg name="extensions" value="php"/> <!-- which extensions to look for -->
<arg name="parallel" value="80"/> <!-- how many parallel processes to run -->
<arg name="colors"/>
<arg name="cache" value=".phpcs.cache"/> <!-- cache the results and don't commit them -->
<arg value="nps"/> <!-- n = ignore warnings, p = show progress -->
<rule ref="PSR12"/>

<file>src</file>

<rule ref="DMS">
<exclude name="SlevomatCodingStandard.Classes.SuperfluousInterfaceNaming.SuperfluousSuffix"/>
<exclude name="SlevomatCodingStandard.Classes.SuperfluousExceptionNaming.SuperfluousSuffix"/>
<exclude name="Squiz.Commenting.FunctionComment.ThrowsNoFullStop"/>
</rule>
</ruleset>


<exclude-pattern>vendor/*</exclude-pattern>
</ruleset>
59 changes: 39 additions & 20 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,21 +1,40 @@
<?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="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>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.3/phpunit.xsd"
beStrictAboutCoverageMetadata="true"
beStrictAboutOutputDuringTests="true"
bootstrap="vendor/autoload.php"
cacheDirectory=".phpunit.cache"
colors="true"
displayDetailsOnTestsThatTriggerDeprecations="true"
displayDetailsOnTestsThatTriggerErrors="true"
displayDetailsOnTestsThatTriggerNotices="true"
displayDetailsOnTestsThatTriggerWarnings="true"
executionOrder="depends,defects"
failOnRisky="true"
failOnWarning="true"
requireCoverageMetadata="false">
<testsuites>
<testsuite name="default">
<directory>tests</directory>
</testsuite>
</testsuites>

<source restrictNotices="true" restrictWarnings="true">
<include>
<directory>src</directory>
</include>
</source>

<logging>
<testdoxHtml outputFile="tests/_reports/testdox.html"/>
</logging>
<coverage includeUncoveredFiles="true"
pathCoverage="false"
ignoreDeprecatedCodeUnits="true"
disableCodeCoverageIgnore="true">
<report>
<html outputDirectory="tests/_reports/html-coverage" lowUpperBound="50" highLowerBound="90"/>
</report>
</coverage>
</phpunit>
15 changes: 9 additions & 6 deletions src/DMS/Filter/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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
) {
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/DMS/Filter/Filters/BooleanScalar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/DMS/Filter/Filters/FloatScalar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/DMS/Filter/Filters/IntScalar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
5 changes: 2 additions & 3 deletions src/DMS/Filter/Filters/RegExp.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/DMS/Filter/Filters/ToLower.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading