Skip to content

Commit c89efa1

Browse files
Merge pull request #6 from brianvarskonst/feature/upgrade-to-php-81
Feature/Upgrade: Codebase to PHP 8.1
2 parents c7bd058 + 1465ead commit c89efa1

19 files changed

+152
-117
lines changed

.github/workflows/testing.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
steps:
1111
- uses: shivammathur/setup-php@v2
1212
with:
13-
php-version: '7.4'
13+
php-version: '8.1'
1414
tools: composer:v1
1515
# Save the current branch name in an environment variable to use it for the conditional logic below
1616
- name: Extract branch name into env

.psalm/autoloader.php

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,4 @@
99
define('DAY_IN_SECONDS', 24 * HOUR_IN_SECONDS);
1010
define('WEEK_IN_SECONDS', 7 * DAY_IN_SECONDS);
1111
define('MONTH_IN_SECONDS', 30 * DAY_IN_SECONDS);
12-
define('YEAR_IN_SECONDS', 365 * DAY_IN_SECONDS);
13-
14-
define('ABSPATH', dirname(__DIR__) . '/vendor/wordpress/wordpress/');
15-
define('WPINC', 'wp-includes');
16-
17-
defined('WP_CONTENT_DIR') or define('WP_CONTENT_DIR', ABSPATH . 'wp-content');
18-
defined('WP_PLUGIN_DIR') or define('WP_PLUGIN_DIR', WP_CONTENT_DIR . '/plugins');
19-
20-
require_once ABSPATH . WPINC . '/pluggable.php';
21-
require_once ABSPATH . WPINC . '/kses.php';
22-
require_once ABSPATH . WPINC . '/functions.php';
12+
define('YEAR_IN_SECONDS', 365 * DAY_IN_SECONDS);

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ which will expire after a certain time - so this needs not to be that big and co
3737
Maybe this will change in the future for the next major version in some years.
3838

3939
## Requirements
40-
- PHP 7.4
40+
- PHP 8.1
4141
- Composer 1 | 2
4242

4343
## Features
@@ -222,18 +222,21 @@ $ composer tests:integration
222222

223223
### Testing Plugin
224224

225+
#### Note: Outdated ATM
226+
225227
If you want to see how this works in the wild, you can have a look at
226228
[WordPress Nonce Manager Test Plugin](https://github.com/brianvarskonst/wordpress-nonce-manager-test-plugin) Repository.
227229

228230
### Credits
229231
* [WordPress Nonces Documentation](https://codex.wordpress.org/WordPress_Nonces)
230232
* [PHPUnit Documentation](https://phpunit.de)
231233
* [Brain-WP / BrainMonkey](https://github.com/Brain-WP/BrainMonkey)
232-
* [Inpsyde Coding Standards](https://github.com/inpsyde/php-coding-standards)
234+
* [Inpsyde - Coding Standards](https://github.com/inpsyde/php-coding-standards)
235+
* [Inpsyde - Psalm WP Stubs](https://github.com/inpsyde/wp-stubs)
233236

234237
## License
235238

236-
Copyright (c) 2022, Brianvarskonst under [MIT](LICENSE) License
239+
Copyright (c) 2023, Brianvarskonst under [MIT](LICENSE) License
237240

238241
## Contributing
239242

composer.json

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"issues": "https://github.com/brianvarskonst/wordpress-nonce/issues"
2222
},
2323
"require": {
24-
"php": ">=7.4"
24+
"php": ">=8.1"
2525
},
2626
"require-dev": {
2727
"inpsyde/php-coding-standards": "^1.0",
@@ -30,7 +30,9 @@
3030
"phpunit/phpunit": "^9.5",
3131
"vimeo/psalm": "^4",
3232
"psalm/plugin-mockery": "^0.9.1",
33-
"roots/wordpress": "5.3.*@stable"
33+
"roots/wordpress": "5.3.*@stable",
34+
"rector/rector": "^0.15.16",
35+
"inpsyde/wp-stubs-versions": "dev-latest"
3436
},
3537
"autoload": {
3638
"psr-4": {
@@ -56,16 +58,27 @@
5658
"@cs",
5759
"@psalm",
5860
"@tests"
59-
]
61+
],
62+
"rector:dry": "@php ./vendor/bin/rector process src --dry-run",
63+
"rector:run": "@php ./vendor/bin/rector process src"
6064
},
6165
"config": {
6266
"optimize-autoloader": true,
6367
"sort-packages": true,
6468
"platform": {
65-
"php": "7.4"
69+
"php": "8.1"
6670
}
6771
},
6872
"extra": {
6973
"wordpress-install-dir": "vendor/wordpress/wordpress"
70-
}
74+
},
75+
"repositories": [
76+
{
77+
"type": "composer",
78+
"url": "https://raw.githubusercontent.com/inpsyde/wp-stubs/main",
79+
"only": [
80+
"inpsyde/wp-stubs-versions"
81+
]
82+
}
83+
]
7184
}

psalm.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
xmlns="https://getpsalm.org/schema/config"
66
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
77
>
8+
<stubs>
9+
<file name="vendor/inpsyde/wp-stubs-versions/latest.php"/>
10+
</stubs>
11+
812
<projectFiles>
913
<directory name="src"/>
1014

rector.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector;
6+
use Rector\Config\RectorConfig;
7+
use Rector\Set\ValueObject\LevelSetList;
8+
use Rector\Set\ValueObject\SetList;
9+
10+
return static function (RectorConfig $rectorConfig): void {
11+
$rectorConfig->paths(
12+
[
13+
__DIR__ . '/src',
14+
__DIR__ . '/tests',
15+
]
16+
);
17+
18+
// define sets of rules
19+
$rectorConfig->sets(
20+
[
21+
LevelSetList::UP_TO_PHP_81,
22+
SetList::DEAD_CODE,
23+
SetList::CODE_QUALITY,
24+
SetList::TYPE_DECLARATION,
25+
SetList::CODING_STYLE,
26+
SetList::EARLY_RETURN,
27+
SetList::PRIVATIZATION,
28+
SetList::NAMING
29+
]
30+
);
31+
};

src/NonceManager.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,15 @@
1515

1616
final class NonceManager
1717
{
18-
private NonceFactory $nonceFactory;
19-
20-
private Verifier $verifier;
21-
2218
public function __construct(
23-
NonceFactory $nonceFactory,
24-
Verifier $verifier
19+
private readonly NonceFactory $nonceFactory,
20+
private readonly Verifier $verifier
2521
) {
26-
27-
$this->nonceFactory = $nonceFactory;
28-
$this->verifier = $verifier;
2922
}
3023

24+
/**
25+
* Create a new NonceManager with delivered components
26+
*/
3127
public static function createFromDefaults(): NonceManager
3228
{
3329
return new NonceManager(

src/Nonces/AbstractNonce.php

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,22 @@
88

99
abstract class AbstractNonce implements Nonce, Stringable
1010
{
11-
/**
12-
* The name of the action
13-
**/
14-
protected string $action;
15-
16-
/**
17-
* The name of the request
18-
**/
19-
protected string $requestName;
20-
21-
/**
22-
* The lifetime of a nonce in seconds
23-
**/
24-
protected int $lifetime;
25-
2611
/**
2712
* cryptographic token
2813
*/
2914
protected string $token;
3015

16+
/**
17+
* @param string $action The name of the action
18+
* @param string $requestName The name of the request
19+
* @param int $lifetime The lifetime of a nonce in seconds
20+
*/
3121
public function __construct(
32-
string $action,
33-
string $requestName,
34-
int $lifetime = DAY_IN_SECONDS
22+
protected string $action,
23+
protected string $requestName,
24+
protected int $lifetime = DAY_IN_SECONDS
3525
) {
3626

37-
$this->action = $action;
38-
$this->requestName = $requestName;
39-
$this->lifetime = $lifetime;
4027
$this->token = wp_create_nonce($action);
4128
}
4229

@@ -71,6 +58,6 @@ public function getToken(): string
7158

7259
public function __toString(): string
7360
{
74-
return $this->getToken();
61+
return $this->token;
7562
}
7663
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
// phpcs:ignoreFile
4+
5+
declare(strict_types=1);
6+
7+
namespace Bvsk\WordPress\NonceManager\Nonces\Config;
8+
9+
use ValueError;
10+
11+
enum DefaultWordPressNonceConfig: string
12+
{
13+
case ACTION = 'action';
14+
case REQUEST_NAME = 'requestName';
15+
16+
case LIFETIME = 'lifetime';
17+
18+
case URL = 'url';
19+
20+
case REFERER = 'referer';
21+
22+
/**
23+
* @throws ValueError
24+
*/
25+
public function getDefault(): string|int
26+
{
27+
return match ($this) {
28+
self::ACTION => '-1',
29+
self::REQUEST_NAME => '_wpnonce',
30+
self::LIFETIME => DAY_IN_SECONDS,
31+
self::URL, self::REFERER => throw new ValueError('No default value are existing.'),
32+
};
33+
}
34+
}

src/Nonces/Factory/AggregatedNonceFactory.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,19 @@
77
use Bvsk\WordPress\NonceManager\Nonces\Nonce;
88
use InvalidArgumentException;
99

10-
class AggregatedNonceFactory implements NonceFactory
10+
final class AggregatedNonceFactory implements NonceFactory
1111
{
12-
private array $types;
12+
private readonly array $types;
1313

14-
/**
15-
* @var NonceFactory[]
16-
*/
17-
private array $childFactories;
14+
private readonly array $factories;
1815

19-
public function __construct(NonceFactory ...$childFactories)
16+
public function __construct(NonceFactory ...$factories)
2017
{
21-
$this->childFactories = $childFactories;
18+
$this->factories = $factories;
2219

2320
$this->types = array_map(
2421
static fn(NonceFactory $factory): string => $factory->getSupportedType(),
25-
$childFactories
22+
$factories
2623
);
2724
}
2825

@@ -41,7 +38,7 @@ public function getSupportedType(): string
4138
*/
4239
public function create(string $type, array $data = []): Nonce
4340
{
44-
foreach ($this->childFactories as $factory) {
41+
foreach ($this->factories as $factory) {
4542
if ($factory->accepts($type, $data)) {
4643
return $factory->create($type, $data);
4744
}

0 commit comments

Comments
 (0)