Skip to content

Commit

Permalink
Refactor PHP codebase and update dependencies (#213)
Browse files Browse the repository at this point in the history
* Refactor PHP codebase and update dependencies

This commit includes a significant refactoring of the entire PHP codebase, consisting of readjustment in importing functions, reordering of code elements, and an upgrade of the dependencies in composer.json file. Moreover, PHPUnit test case annotations are updated to attributes and phpunit.xml configuration file is simplified. It also provides a shift toward more modern PHP coding style, making the code more readable and maintainable.
  • Loading branch information
Spomky authored Apr 14, 2024
1 parent bc0167e commit cf52e18
Show file tree
Hide file tree
Showing 15 changed files with 237 additions and 351 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/infection.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# yaml-language-server: $schema=https://json.schemastore.org/github-workflow

name: "Infection"

on:
push:
branches:
- "*.x"

jobs:
mutation_testing:
name: "5️⃣ Mutation Testing"
steps:
- name: "Set up PHP"
uses: "shivammathur/setup-php@v2"
with:
php-version: "8.1"
extensions: "mbstring"
coverage: "xdebug"

- name: "Checkout code"
uses: "actions/checkout@v3"

- name: "Fetch Git base reference"
run: "git fetch --depth=1 origin ${GITHUB_BASE_REF}"

- name: "Install dependencies"
uses: "ramsey/composer-install@v2"
with:
dependency-versions: "highest"
composer-options: "--optimize-autoloader"

- name: "Execute Infection"
run: "make ci-mu"
39 changes: 2 additions & 37 deletions .github/workflows/integrate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ jobs:
- "ubuntu-latest"
php-version:
- "8.1"
- "8.2"
- "8.3"
dependencies:
- "lowest"
- "highest"
Expand All @@ -80,14 +82,6 @@ jobs:
- name: "Execute tests (PHP)"
run: "make ci-cc"

# - name: Send coverage to Coveralls
# if: "matrix.php-version == '8.1' && matrix.dependencies == 'highest'"
# env:
# COVERALLS_REPO_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
# run: |
# wget "https://github.com/php-coveralls/php-coveralls/releases/download/v2.5.2/php-coveralls.phar"
# php ./php-coveralls.phar -v

static_analysis:
name: "3️⃣ Static Analysis"
needs:
Expand Down Expand Up @@ -153,35 +147,6 @@ jobs:
run: |
vendor/bin/deptrac analyse --fail-on-uncovered --no-cache
mutation_testing:
name: "5️⃣ Mutation Testing"
needs:
- "byte_level"
- "syntax_errors"
runs-on: "ubuntu-latest"
steps:
- name: "Set up PHP"
uses: "shivammathur/setup-php@v2"
with:
php-version: "8.1"
extensions: "mbstring"
coverage: "xdebug"

- name: "Checkout code"
uses: "actions/checkout@v3"

- name: "Fetch Git base reference"
run: "git fetch --depth=1 origin ${GITHUB_BASE_REF}"

- name: "Install dependencies"
uses: "ramsey/composer-install@v2"
with:
dependency-versions: "highest"
composer-options: "--optimize-autoloader"

- name: "Execute Infection"
run: "make ci-mu"

rector_checkstyle:
name: "6️⃣ Rector Checkstyle"
needs:
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ TOTP / HOTP library in PHP
==========================

![Build Status](https://github.com/spomky-labs/otphp/workflows/Integrate/badge.svg)
![Build Status](https://github.com/spomky-labs/otphp/workflows/Infection/badge.svg)

[![Latest Stable Version](https://poser.pugx.org/spomky-labs/otphp/v/stable.png)](https://packagist.org/packages/spomky-labs/otphp)
[![Total Downloads](https://poser.pugx.org/spomky-labs/otphp/downloads.png)](https://packagist.org/packages/spomky-labs/otphp)
Expand Down
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@
},
"require-dev": {
"ekino/phpstan-banned-code": "^1.0",
"infection/infection": "^0.26",
"infection/infection": "^0.26|^0.27|^0.28",
"php-parallel-lint/php-parallel-lint": "^1.3",
"phpstan/phpstan": "^1.0",
"phpstan/phpstan-deprecation-rules": "^1.0",
"phpstan/phpstan-phpunit": "^1.0",
"phpstan/phpstan-strict-rules": "^1.0",
"phpunit/phpunit": "^9.5.26",
"phpunit/phpunit": "^9.5.26|^10.0|^11.0",
"qossmic/deptrac-shim": "^1.0",
"rector/rector": "^0.15",
"symfony/phpunit-bridge": "^6.1",
"symplify/easy-coding-standard": "^11.0"
"rector/rector": "1.0",
"symfony/phpunit-bridge": "^6.1|^7.0",
"symplify/easy-coding-standard": "^12.0"
},
"autoload": {
"psr-4": { "OTPHP\\": "src/" }
Expand Down
16 changes: 4 additions & 12 deletions ecs.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@
use Symplify\EasyCodingStandard\Config\ECSConfig;
use Symplify\EasyCodingStandard\ValueObject\Set\SetList;

$header = '';

return static function (ECSConfig $config) use ($header): void {
return static function (ECSConfig $config): void {
$config->import(SetList::PSR_12);
$config->import(SetList::CLEAN_CODE);
$config->import(SetList::DOCTRINE_ANNOTATIONS);
Expand Down Expand Up @@ -74,7 +72,7 @@
'strict' => true,
]);
$config->ruleWithConfiguration(HeaderCommentFixer::class, [
'header' => $header,
'header' => '',
]);
$config->ruleWithConfiguration(AlignMultilineCommentFixer::class, [
'comment_type' => 'all_multiline',
Expand All @@ -88,13 +86,7 @@
'import_functions' => true,
]);

$config->services()
->remove(PhpUnitTestClassRequiresCoversFixer::class)
;

$config->parallel();
$config->paths([
__DIR__.'/src',
__DIR__.'/tests',
]);
$config->paths([__DIR__]);
$config->skip([__DIR__ . '/vendor', PhpUnitTestClassRequiresCoversFixer::class]);
};
22 changes: 6 additions & 16 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,16 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
bootstrap="vendor/autoload.php"
colors="true"
resolveDependencies="true"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
>
<coverage>
<include>
<directory suffix=".php">./src</directory>
</include>
</coverage>
<testsuites>
<testsuite name="OTP Test Suite">
<directory>./tests</directory>
</testsuite>
</testsuites>
<listeners>
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener">
<arguments>
<array>
<element key="time-sensitive"><string>OTPHP\TOTP</string></element>
</array>
</arguments>
</listener>
</listeners>
<source>
<include>
<directory>./src</directory>
</include>
</source>
</phpunit>
32 changes: 10 additions & 22 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,24 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Core\ValueObject\PhpVersion;
use Rector\Doctrine\Set\DoctrineSetList;
use Rector\Php74\Rector\Property\TypedPropertyRector;
use Rector\PHPUnit\Set\PHPUnitLevelSetList;
use Rector\PHPUnit\CodeQuality\Rector\Class_\PreferPHPUnitThisCallRector;
use Rector\PHPUnit\Set\PHPUnitSetList;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Set\ValueObject\SetList;
use Rector\Symfony\Set\SymfonyLevelSetList;
use Rector\Symfony\Set\SymfonySetList;
use Rector\ValueObject\PhpVersion;

return static function (RectorConfig $config): void {
$config->sets([
SetList::DEAD_CODE,
LevelSetList::UP_TO_PHP_81,
SymfonyLevelSetList::UP_TO_SYMFONY_54,
SymfonySetList::SYMFONY_CODE_QUALITY,
SymfonySetList::SYMFONY_CONSTRUCTOR_INJECTION,
DoctrineSetList::DOCTRINE_CODE_QUALITY,
DoctrineSetList::ANNOTATIONS_TO_ATTRIBUTES,
PHPUnitSetList::PHPUNIT_SPECIFIC_METHOD,
PHPUnitLevelSetList::UP_TO_PHPUNIT_100,
PHPUnitSetList::PHPUNIT_CODE_QUALITY,
PHPUnitSetList::PHPUNIT_EXCEPTION,
PHPUnitSetList::REMOVE_MOCKS,
PHPUnitSetList::PHPUNIT_YIELD_DATA_PROVIDER,
]);
$config->import(SetList::DEAD_CODE);
$config->import(LevelSetList::UP_TO_PHP_80);
$config->import(SymfonySetList::SYMFONY_CODE_QUALITY);
$config->import(PHPUnitSetList::PHPUNIT_100);
$config->import(PHPUnitSetList::ANNOTATIONS_TO_ATTRIBUTES);
$config->import(PHPUnitSetList::PHPUNIT_CODE_QUALITY);
$config->parallel();
$config->paths([__DIR__ . '/src']);
$config->paths([__DIR__ . '/src', __DIR__ . '/tests']);
$config->skip([PreferPHPUnitThisCallRector::class]);
$config->phpVersion(PhpVersion::PHP_81);
$config->importNames();
$config->importShortClasses();

};
4 changes: 2 additions & 2 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace OTPHP;

use function assert;
use function count;
use InvalidArgumentException;
use Throwable;
use function assert;
use function count;

/**
* This class is used to load OTP object from a provisioning Uri.
Expand Down
8 changes: 4 additions & 4 deletions src/OTP.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

namespace OTPHP;

use function assert;
use function chr;
use function count;
use Exception;
use InvalidArgumentException;
use function is_string;
use ParagonIE\ConstantTime\Base32;
use RuntimeException;
use function assert;
use function chr;
use function count;
use function is_string;
use const STR_PAD_LEFT;

abstract class OTP implements OTPInterface
Expand Down
2 changes: 1 addition & 1 deletion src/ParameterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace OTPHP;

use InvalidArgumentException;
use function array_key_exists;
use function assert;
use function in_array;
use InvalidArgumentException;
use function is_int;
use function is_string;

Expand Down
34 changes: 16 additions & 18 deletions src/TOTP.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace OTPHP;

use function assert;
use InvalidArgumentException;
use function assert;
use function is_int;

/**
Expand Down Expand Up @@ -133,23 +133,21 @@ public function setEpoch(int $epoch): void
*/
protected function getParameterMap(): array
{
return array_merge(
parent::getParameterMap(),
[
'period' => static function ($value): int {
(int) $value > 0 || throw new InvalidArgumentException('Period must be at least 1.');

return (int) $value;
},
'epoch' => static function ($value): int {
(int) $value >= 0 || throw new InvalidArgumentException(
'Epoch must be greater than or equal to 0.'
);

return (int) $value;
},
]
);
return [
...parent::getParameterMap(),
'period' => static function ($value): int {
(int) $value > 0 || throw new InvalidArgumentException('Period must be at least 1.');

return (int) $value;
},
'epoch' => static function ($value): int {
(int) $value >= 0 || throw new InvalidArgumentException(
'Epoch must be greater than or equal to 0.'
);

return (int) $value;
},
];
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace OTPHP;

use function array_key_exists;
use InvalidArgumentException;
use function array_key_exists;
use function is_string;

/**
Expand Down
Loading

0 comments on commit cf52e18

Please sign in to comment.