mirror of
https://github.com/pestphp/pest.git
synced 2026-03-12 10:47:25 +01:00
feat(presets): reworks code
This commit is contained in:
@ -5,21 +5,27 @@ declare(strict_types=1);
|
||||
namespace Pest\ArchPresets;
|
||||
|
||||
use Pest\Arch\Contracts\ArchExpectation;
|
||||
use Pest\Expectation;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
abstract class AbstractPreset
|
||||
{
|
||||
/**
|
||||
* The expectations.
|
||||
*
|
||||
* @var array<int, ArchExpectation>
|
||||
*/
|
||||
protected array $expectations = [];
|
||||
|
||||
/**
|
||||
* Creates a new preset instance.
|
||||
*
|
||||
* @param array<int, string> $userNamespaces
|
||||
* @param array<int, ArchExpectation> $expectations
|
||||
*/
|
||||
final public function __construct(// @phpstan-ignore-line
|
||||
protected array $userNamespaces,
|
||||
protected array $expectations = [],
|
||||
final public function __construct(
|
||||
private readonly array $userNamespaces,
|
||||
) {
|
||||
//
|
||||
}
|
||||
@ -44,6 +50,20 @@ abstract class AbstractPreset
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs the given callback for each namespace.
|
||||
*
|
||||
* @param callable(Expectation<string|null>): ArchExpectation ...$callbacks
|
||||
*/
|
||||
final public function eachUserNamespace(callable ...$callbacks): void
|
||||
{
|
||||
foreach ($this->userNamespaces as $namespace) {
|
||||
foreach ($callbacks as $callback) {
|
||||
$this->expectations[] = $callback(expect($namespace));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Flushes the expectations.
|
||||
*/
|
||||
|
||||
@ -7,7 +7,7 @@ namespace Pest\ArchPresets;
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
final class Base extends AbstractPreset
|
||||
final class Php extends AbstractPreset
|
||||
{
|
||||
/**
|
||||
* Executes the arch preset.
|
||||
25
src/ArchPresets/Relaxed.php
Normal file
25
src/ArchPresets/Relaxed.php
Normal file
@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Pest\ArchPresets;
|
||||
|
||||
use Pest\Arch\Contracts\ArchExpectation;
|
||||
use Pest\Expectation;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
final class Relaxed extends AbstractPreset
|
||||
{
|
||||
/**
|
||||
* Executes the arch preset.
|
||||
*/
|
||||
public function execute(): void
|
||||
{
|
||||
$this->eachUserNamespace(
|
||||
fn (Expectation $namespace): ArchExpectation => $namespace->not->toUseStrictTypes(),
|
||||
fn (Expectation $namespace): ArchExpectation => $namespace->classes()->not->toBeFinal(),
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -4,6 +4,9 @@ declare(strict_types=1);
|
||||
|
||||
namespace Pest\ArchPresets;
|
||||
|
||||
use Pest\Arch\Contracts\ArchExpectation;
|
||||
use Pest\Expectation;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
@ -14,6 +17,10 @@ final class Security extends AbstractPreset
|
||||
*/
|
||||
public function execute(): void
|
||||
{
|
||||
$this->eachUserNamespace(
|
||||
fn (Expectation $namespace): ArchExpectation => $namespace->not->toHaveFileSystemPermissions('0777'),
|
||||
);
|
||||
|
||||
$this->expectations[] = expect([
|
||||
'md5',
|
||||
'sha1',
|
||||
@ -37,11 +44,5 @@ final class Security extends AbstractPreset
|
||||
'dl',
|
||||
'assert',
|
||||
])->not->toBeUsed();
|
||||
|
||||
foreach ($this->userNamespaces as $namespace) {
|
||||
$this->expectations[] = expect($namespace)
|
||||
->not
|
||||
->toHaveFileSystemPermissions('0777');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -4,6 +4,9 @@ declare(strict_types=1);
|
||||
|
||||
namespace Pest\ArchPresets;
|
||||
|
||||
use Pest\Arch\Contracts\ArchExpectation;
|
||||
use Pest\Expectation;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
@ -14,14 +17,15 @@ final class Strict extends AbstractPreset
|
||||
*/
|
||||
public function execute(): void
|
||||
{
|
||||
$this->eachUserNamespace(
|
||||
fn (Expectation $namespace): ArchExpectation => $namespace->toUseStrictTypes(),
|
||||
fn (Expectation $namespace): ArchExpectation => $namespace->classes()->toBeFinal(),
|
||||
fn (Expectation $namespace): ArchExpectation => $namespace->classes()->not->toBeAbstract(),
|
||||
);
|
||||
|
||||
$this->expectations[] = expect([
|
||||
'sleep',
|
||||
'usleep',
|
||||
])->not->toBeUsed();
|
||||
|
||||
foreach ($this->userNamespaces as $namespace) {
|
||||
$this->expectations[] = expect($namespace)
|
||||
->toUseStrictTypes();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user