Skip to content

Commit

Permalink
ci: Added PHP CS Fixer
Browse files Browse the repository at this point in the history
  • Loading branch information
yohang committed Jan 15, 2025
1 parent 549bfc8 commit 7661f96
Show file tree
Hide file tree
Showing 47 changed files with 274 additions and 159 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,24 @@ on:
pull_request:

jobs:
cs:
runs-on: ubuntu-latest
name: Code Style
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.3'

- name: Install dependencies
uses: ramsey/composer-install@v3

- name: PHP CS Fixer
run: ./vendor/bin/php-cs-fixer fix --dry-run --diff --ansi

psalm:
runs-on: ubuntu-latest
permissions:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
tests/Extension/Symfony/Fixtures/app/var/
vendor/
.phpunit.result.cache
.php-cs-fixer.cache
composer.lock
35 changes: 35 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

$finder = PhpCsFixer\Finder::create()
->in(__DIR__)
->exclude('tests/Extension/Symfony/Fixtures/app/var')
->exclude('vendor');

return (new PhpCsFixer\Config())
->setRiskyAllowed(true)
->setRules([
'@Symfony' => true,
'@Symfony:risky' => true,
'array_syntax' => [
'syntax' => 'short',
],
'braces' => [
'allow_single_line_closure' => true,
],
'declare_strict_types' => true,
'modernize_types_casting' => true,
'native_function_invocation' => ['include' => ['@compiler_optimized'], 'scope' => 'namespaced'],
'no_useless_else' => true,
'no_useless_return' => true,
'ordered_imports' => true,
'phpdoc_add_missing_param_annotation' => [
'only_untyped' => true,
],
'phpdoc_order' => true,
'phpdoc_to_comment' => false,
'semicolon_after_instruction' => true,
'strict_comparison' => true,
'strict_param' => true,
'ternary_to_null_coalescing' => true,
])
->setFinder($finder);
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
"symfony/twig-bundle": ">=5.4,<8",
"symfony/browser-kit": ">=5.4,<8",
"symfony/css-selector": ">=5.4,<8",
"symfony/console": ">=5.4,<8"
"symfony/console": ">=5.4,<8",
"friendsofphp/php-cs-fixer": "^3.68"
},
"autoload": {
"psr-4": {
Expand Down
24 changes: 12 additions & 12 deletions examples/basic-graph.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
#!/usr/bin/env php
<?php

declare(strict_types=1);

use Finite\State;
use Finite\Transition\Transition;

require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__.'/../vendor/autoload.php';

// Implement your State
enum DocumentState: string implements State
{
const PUBLISH = 'publish';
const CLEAR = 'clear';
const REPORT = 'report';
const DISABLE = 'disable';
public const PUBLISH = 'publish';
public const CLEAR = 'clear';
public const REPORT = 'report';
public const DISABLE = 'disable';

case DRAFT = 'draft';
case PUBLISHED = 'published';
Expand All @@ -21,12 +23,12 @@ enum DocumentState: string implements State

public function isDeletable(): bool
{
return in_array($this, [self::DRAFT, self::DISABLED]);
return in_array($this, [self::DRAFT, self::DISABLED], true);
}

public function isPrintable(): bool
{
return in_array($this, [self::PUBLISHED, self::REPORTED]);
return in_array($this, [self::PUBLISHED, self::REPORTED], true);
}

public static function getTransitions(): array
Expand Down Expand Up @@ -57,16 +59,14 @@ public function setState(DocumentState $state): void
}

// Configure your graph
$document = new Document;
$stateMachine = new Finite\StateMachine;

$document = new Document();
$stateMachine = new Finite\StateMachine();

// Working with workflow

// Current state
var_dump($document->getState());


// Available transitions
var_dump($stateMachine->getReachablesTransitions($document));
var_dump($stateMachine->can($document, DocumentState::PUBLISH));
Expand All @@ -77,7 +77,7 @@ public function setState(DocumentState $state): void
// Apply transitions
try {
$stateMachine->apply($document, DocumentState::REPORT);
} catch (\Exception $e) {
} catch (Exception $e) {
echo $e->getMessage(), "\n";
}

Expand Down
2 changes: 2 additions & 0 deletions src/Dumper/Dumper.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Finite\Dumper;

use Finite\State;
Expand Down
8 changes: 5 additions & 3 deletions src/Dumper/MermaidDumper.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Finite\Dumper;

use Finite\State;
Expand All @@ -13,14 +15,14 @@ public function dump(string $stateEnum): string
{
$output = [
'---',
'title: ' . $stateEnum,
'title: '.$stateEnum,
'---',
'stateDiagram-v2',
];

foreach ($stateEnum::getTransitions() as $transition) {
foreach ($transition->getSourceStates() as $state) {
$output[] = sprintf(
$output[] = \sprintf(
' %s --> %s: %s',
$state->value,
$transition->getTargetState()->value,
Expand All @@ -29,6 +31,6 @@ public function dump(string $stateEnum): string
}
}

return implode(PHP_EOL, $output);
return implode(\PHP_EOL, $output);
}
}
2 changes: 2 additions & 0 deletions src/Event/CanTransitionEvent.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Finite\Event;

final class CanTransitionEvent extends TransitionEvent
Expand Down
5 changes: 3 additions & 2 deletions src/Event/Event.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Finite\Event;

use Psr\EventDispatcher\StoppableEventInterface;
Expand All @@ -11,8 +13,7 @@ abstract class Event implements StoppableEventInterface
public function __construct(
private readonly object $object,
private readonly ?string $stateClass = null,
)
{
) {
}

public function getObject(): object
Expand Down
6 changes: 4 additions & 2 deletions src/Event/EventDispatcher.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Finite\Event;

use Psr\EventDispatcher\EventDispatcherInterface;
Expand All @@ -23,11 +25,11 @@ public function addEventListener(string $eventClass, callable $listener): void

public function dispatch(object $event): void
{
if (!isset($this->listeners[get_class($event)])) {
if (!isset($this->listeners[$event::class])) {
return;
}

foreach ($this->listeners[get_class($event)] as $listener) {
foreach ($this->listeners[$event::class] as $listener) {
$listener($event);

if ($event instanceof StoppableEventInterface && $event->isPropagationStopped()) {
Expand Down
2 changes: 2 additions & 0 deletions src/Event/PostTransitionEvent.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Finite\Event;

final class PostTransitionEvent extends TransitionEvent
Expand Down
2 changes: 2 additions & 0 deletions src/Event/PreTransitionEvent.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Finite\Event;

final class PreTransitionEvent extends TransitionEvent
Expand Down
5 changes: 3 additions & 2 deletions src/Event/TransitionEvent.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Finite\Event;

abstract class TransitionEvent extends Event
Expand All @@ -8,8 +10,7 @@ public function __construct(
object $object,
private readonly string $transitionName,
?string $stateClass = null,
)
{
) {
parent::__construct($object, $stateClass);
}

Expand Down
3 changes: 2 additions & 1 deletion src/Exception/BadStateClassException.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php

declare(strict_types=1);

namespace Finite\Exception;

class BadStateClassException extends \InvalidArgumentException implements FiniteException
{

}
2 changes: 2 additions & 0 deletions src/Exception/FiniteException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Finite\Exception;

interface FiniteException
Expand Down
2 changes: 2 additions & 0 deletions src/Exception/NoStateFoundException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Finite\Exception;

class NoStateFoundException extends \InvalidArgumentException implements FiniteException
Expand Down
2 changes: 2 additions & 0 deletions src/Exception/NonUniqueStateException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Finite\Exception;

final class NonUniqueStateException extends \InvalidArgumentException implements FiniteException
Expand Down
2 changes: 2 additions & 0 deletions src/Exception/PropertyNotFoundException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Finite\Exception;

class PropertyNotFoundException extends \OutOfBoundsException implements FiniteException
Expand Down
2 changes: 2 additions & 0 deletions src/Exception/TransitionNotReachableException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Finite\Exception;

final class TransitionNotReachableException extends \InvalidArgumentException implements FiniteException
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

declare(strict_types=1);

namespace Finite\Extension\Symfony\Bundle\DependencyInjection;
Expand Down
3 changes: 2 additions & 1 deletion src/Extension/Symfony/Bundle/FiniteBundle.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

declare(strict_types=1);

namespace Finite\Extension\Symfony\Bundle;
Expand All @@ -11,6 +12,6 @@ final class FiniteBundle extends Bundle
{
public function registerCommands(Application $application): void
{
$application->add(new DumpStateMachineCommand);
$application->add(new DumpStateMachineCommand());
}
}
12 changes: 7 additions & 5 deletions src/Extension/Symfony/Command/DumpStateMachineCommand.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Finite\Extension\Symfony\Command;

use Finite\Dumper\MermaidDumper;
Expand Down Expand Up @@ -27,23 +29,23 @@ protected function configure(): void
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$stateEnum = (string)$input->getArgument('state_enum');
$stateEnum = (string) $input->getArgument('state_enum');

if (!(enum_exists($stateEnum) && is_subclass_of($stateEnum, State::class))) {
$io->error('The state enum "' . $stateEnum . '" does not exist.');
$io->error('The state enum "'.$stateEnum.'" does not exist.');

return self::FAILURE;
}

$format = (string)$input->getArgument('format');
$format = (string) $input->getArgument('format');
switch ($format) {
case self::FORMAT_MERMAID:
/** @psalm-suppress ArgumentTypeCoercion Type is enforced upper but not detected by psalm */
$output->writeln((new MermaidDumper)->dump($stateEnum));
$output->writeln((new MermaidDumper())->dump($stateEnum));

break;
default:
$output->writeln('Unknown format "' . $format . '". Supported formats are: ' . implode(', ', self::FORMATS));
$output->writeln('Unknown format "'.$format.'". Supported formats are: '.implode(', ', self::FORMATS));

return self::FAILURE;
}
Expand Down
5 changes: 3 additions & 2 deletions src/Extension/Twig/FiniteExtension.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Finite\Extension\Twig;

use Finite\StateMachine;
Expand All @@ -10,8 +12,7 @@ class FiniteExtension extends AbstractExtension
{
public function __construct(
private readonly StateMachine $stateMachine,
)
{
) {
}

public function getFunctions(): array
Expand Down
Loading

0 comments on commit 7661f96

Please sign in to comment.