Skip to content

Commit

Permalink
Merge pull request #6 from brianvarskonst/feature/upgrade-to-php-81
Browse files Browse the repository at this point in the history
Feature/Upgrade: Codebase to PHP 8.1
  • Loading branch information
brianvarskonst authored Feb 18, 2023
2 parents c7bd058 + 1465ead commit c89efa1
Show file tree
Hide file tree
Showing 19 changed files with 152 additions and 117 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/testing.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
steps:
- uses: shivammathur/setup-php@v2
with:
php-version: '7.4'
php-version: '8.1'
tools: composer:v1
# Save the current branch name in an environment variable to use it for the conditional logic below
- name: Extract branch name into env
Expand Down
12 changes: 1 addition & 11 deletions .psalm/autoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,4 @@
define('DAY_IN_SECONDS', 24 * HOUR_IN_SECONDS);
define('WEEK_IN_SECONDS', 7 * DAY_IN_SECONDS);
define('MONTH_IN_SECONDS', 30 * DAY_IN_SECONDS);
define('YEAR_IN_SECONDS', 365 * DAY_IN_SECONDS);

define('ABSPATH', dirname(__DIR__) . '/vendor/wordpress/wordpress/');
define('WPINC', 'wp-includes');

defined('WP_CONTENT_DIR') or define('WP_CONTENT_DIR', ABSPATH . 'wp-content');
defined('WP_PLUGIN_DIR') or define('WP_PLUGIN_DIR', WP_CONTENT_DIR . '/plugins');

require_once ABSPATH . WPINC . '/pluggable.php';
require_once ABSPATH . WPINC . '/kses.php';
require_once ABSPATH . WPINC . '/functions.php';
define('YEAR_IN_SECONDS', 365 * DAY_IN_SECONDS);
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ which will expire after a certain time - so this needs not to be that big and co
Maybe this will change in the future for the next major version in some years.

## Requirements
- PHP 7.4
- PHP 8.1
- Composer 1 | 2

## Features
Expand Down Expand Up @@ -222,18 +222,21 @@ $ composer tests:integration

### Testing Plugin

#### Note: Outdated ATM

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

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

## License

Copyright (c) 2022, Brianvarskonst under [MIT](LICENSE) License
Copyright (c) 2023, Brianvarskonst under [MIT](LICENSE) License

## Contributing

Expand Down
23 changes: 18 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"issues": "https://github.com/brianvarskonst/wordpress-nonce/issues"
},
"require": {
"php": ">=7.4"
"php": ">=8.1"
},
"require-dev": {
"inpsyde/php-coding-standards": "^1.0",
Expand All @@ -30,7 +30,9 @@
"phpunit/phpunit": "^9.5",
"vimeo/psalm": "^4",
"psalm/plugin-mockery": "^0.9.1",
"roots/wordpress": "5.3.*@stable"
"roots/wordpress": "5.3.*@stable",
"rector/rector": "^0.15.16",
"inpsyde/wp-stubs-versions": "dev-latest"
},
"autoload": {
"psr-4": {
Expand All @@ -56,16 +58,27 @@
"@cs",
"@psalm",
"@tests"
]
],
"rector:dry": "@php ./vendor/bin/rector process src --dry-run",
"rector:run": "@php ./vendor/bin/rector process src"
},
"config": {
"optimize-autoloader": true,
"sort-packages": true,
"platform": {
"php": "7.4"
"php": "8.1"
}
},
"extra": {
"wordpress-install-dir": "vendor/wordpress/wordpress"
}
},
"repositories": [
{
"type": "composer",
"url": "https://raw.githubusercontent.com/inpsyde/wp-stubs/main",
"only": [
"inpsyde/wp-stubs-versions"
]
}
]
}
4 changes: 4 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
>
<stubs>
<file name="vendor/inpsyde/wp-stubs-versions/latest.php"/>
</stubs>

<projectFiles>
<directory name="src"/>

Expand Down
31 changes: 31 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

use Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector;
use Rector\Config\RectorConfig;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Set\ValueObject\SetList;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->paths(
[
__DIR__ . '/src',
__DIR__ . '/tests',
]
);

// define sets of rules
$rectorConfig->sets(
[
LevelSetList::UP_TO_PHP_81,
SetList::DEAD_CODE,
SetList::CODE_QUALITY,
SetList::TYPE_DECLARATION,
SetList::CODING_STYLE,
SetList::EARLY_RETURN,
SetList::PRIVATIZATION,
SetList::NAMING
]
);
};
14 changes: 5 additions & 9 deletions src/NonceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,15 @@

final class NonceManager
{
private NonceFactory $nonceFactory;

private Verifier $verifier;

public function __construct(
NonceFactory $nonceFactory,
Verifier $verifier
private readonly NonceFactory $nonceFactory,
private readonly Verifier $verifier
) {

$this->nonceFactory = $nonceFactory;
$this->verifier = $verifier;
}

/**
* Create a new NonceManager with delivered components
*/
public static function createFromDefaults(): NonceManager
{
return new NonceManager(
Expand Down
31 changes: 9 additions & 22 deletions src/Nonces/AbstractNonce.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,22 @@

abstract class AbstractNonce implements Nonce, Stringable
{
/**
* The name of the action
**/
protected string $action;

/**
* The name of the request
**/
protected string $requestName;

/**
* The lifetime of a nonce in seconds
**/
protected int $lifetime;

/**
* cryptographic token
*/
protected string $token;

/**
* @param string $action The name of the action
* @param string $requestName The name of the request
* @param int $lifetime The lifetime of a nonce in seconds
*/
public function __construct(
string $action,
string $requestName,
int $lifetime = DAY_IN_SECONDS
protected string $action,
protected string $requestName,
protected int $lifetime = DAY_IN_SECONDS
) {

$this->action = $action;
$this->requestName = $requestName;
$this->lifetime = $lifetime;
$this->token = wp_create_nonce($action);
}

Expand Down Expand Up @@ -71,6 +58,6 @@ public function getToken(): string

public function __toString(): string
{
return $this->getToken();
return $this->token;
}
}
34 changes: 34 additions & 0 deletions src/Nonces/Config/DefaultWordPressNonceConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

// phpcs:ignoreFile

declare(strict_types=1);

namespace Bvsk\WordPress\NonceManager\Nonces\Config;

use ValueError;

enum DefaultWordPressNonceConfig: string
{
case ACTION = 'action';
case REQUEST_NAME = 'requestName';

case LIFETIME = 'lifetime';

case URL = 'url';

case REFERER = 'referer';

/**
* @throws ValueError
*/
public function getDefault(): string|int
{
return match ($this) {
self::ACTION => '-1',
self::REQUEST_NAME => '_wpnonce',
self::LIFETIME => DAY_IN_SECONDS,
self::URL, self::REFERER => throw new ValueError('No default value are existing.'),
};
}
}
17 changes: 7 additions & 10 deletions src/Nonces/Factory/AggregatedNonceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,19 @@
use Bvsk\WordPress\NonceManager\Nonces\Nonce;
use InvalidArgumentException;

class AggregatedNonceFactory implements NonceFactory
final class AggregatedNonceFactory implements NonceFactory
{
private array $types;
private readonly array $types;

/**
* @var NonceFactory[]
*/
private array $childFactories;
private readonly array $factories;

public function __construct(NonceFactory ...$childFactories)
public function __construct(NonceFactory ...$factories)
{
$this->childFactories = $childFactories;
$this->factories = $factories;

$this->types = array_map(
static fn(NonceFactory $factory): string => $factory->getSupportedType(),
$childFactories
$factories
);
}

Expand All @@ -41,7 +38,7 @@ public function getSupportedType(): string
*/
public function create(string $type, array $data = []): Nonce
{
foreach ($this->childFactories as $factory) {
foreach ($this->factories as $factory) {
if ($factory->accepts($type, $data)) {
return $factory->create($type, $data);
}
Expand Down
12 changes: 0 additions & 12 deletions src/Nonces/Factory/DefaultNonceProperties.php

This file was deleted.

9 changes: 5 additions & 4 deletions src/Nonces/Factory/FieldNonceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Bvsk\WordPress\NonceManager\Nonces\FieldNonce;
use Bvsk\WordPress\NonceManager\Nonces\Nonce;
use Bvsk\WordPress\NonceManager\Nonces\Config\DefaultWordPressNonceConfig as Config;

class FieldNonceFactory extends SimpleNonceFactory
{
Expand All @@ -17,10 +18,10 @@ public function getSupportedType(): string
public function create(string $type, array $data = []): Nonce
{
return new FieldNonce(
$data['action'] ?? DefaultNonceProperties::ACTION,
$data['requestName'] ?? DefaultNonceProperties::REQUEST_NAME,
$this->generateLifetime($data['lifetime'] ?? DefaultNonceProperties::LIFETIME),
(bool) $data['referer']
$data[Config::ACTION->value] ?? Config::ACTION->getDefault(),
$data[Config::REQUEST_NAME->value] ?? Config::REQUEST_NAME->getDefault(),
$this->generateLifetime($data[Config::LIFETIME->value] ?? Config::LIFETIME->getDefault()),
(bool) $data[Config::REFERER->value]
);
}
}
7 changes: 4 additions & 3 deletions src/Nonces/Factory/SimpleNonceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Bvsk\WordPress\NonceManager\Nonces\Nonce;
use Bvsk\WordPress\NonceManager\Nonces\SimpleNonce;
use Bvsk\WordPress\NonceManager\Nonces\Config\DefaultWordPressNonceConfig as Config;

class SimpleNonceFactory implements NonceFactory
{
Expand All @@ -22,9 +23,9 @@ public function getSupportedType(): string
public function create(string $type, array $data = []): Nonce
{
return new SimpleNonce(
$data['action'] ?? DefaultNonceProperties::ACTION,
$data['requestName'] ?? DefaultNonceProperties::REQUEST_NAME,
$this->generateLifetime($data['lifetime'] ?? DefaultNonceProperties::LIFETIME)
$data[Config::ACTION->value] ?? Config::ACTION->getDefault(),
$data[Config::REQUEST_NAME->value] ?? Config::REQUEST_NAME->getDefault(),
$this->generateLifetime($data[Config::LIFETIME->value] ?? Config::LIFETIME->getDefault())
);
}

Expand Down
9 changes: 5 additions & 4 deletions src/Nonces/Factory/UrlNonceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Bvsk\WordPress\NonceManager\Nonces\Nonce;
use Bvsk\WordPress\NonceManager\Nonces\UrlNonce;
use Bvsk\WordPress\NonceManager\Nonces\Config\DefaultWordPressNonceConfig as Config;

class UrlNonceFactory extends SimpleNonceFactory
{
Expand All @@ -26,10 +27,10 @@ public function getSupportedType(): string
public function create(string $type, array $data = []): Nonce
{
return new UrlNonce(
$data['url'],
$data['action'] ?? DefaultNonceProperties::ACTION,
$data['requestName'] ?? DefaultNonceProperties::REQUEST_NAME,
$this->generateLifetime($data['lifetime'] ?? DefaultNonceProperties::LIFETIME),
$data[Config::URL->value],
$data[Config::ACTION->value] ?? Config::ACTION->getDefault(),
$data[Config::REQUEST_NAME->value] ?? Config::REQUEST_NAME->getDefault(),
$this->generateLifetime($data[Config::LIFETIME->value] ?? Config::LIFETIME->getDefault()),
);
}
}
Loading

0 comments on commit c89efa1

Please sign in to comment.