Skip to content

Commit

Permalink
Merge pull request #2 from cgauge/upgrade-php-packages
Browse files Browse the repository at this point in the history
Upgrade PHP and Packages
  • Loading branch information
abdala authored Jun 19, 2023
2 parents e9cd716 + ae65551 commit a4a062f
Show file tree
Hide file tree
Showing 20 changed files with 125 additions and 129 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Tests

env:
PHP_VERSION: 8.2

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
tests:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- uses: shivammathur/setup-php@v2
with:
php-version: ${{ env.PHP_VERSION }}

- name: Install dependencies
run: composer install --prefer-dist --no-progress --no-suggest

- name: Test
run: ./vendor/bin/phpunit
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ composer.phar
composer.lock
/vendor/
.phpunit.result.cache
.phpunit.cache
.phpcs-cache
22 changes: 0 additions & 22 deletions .scrutinizer.yml

This file was deleted.

12 changes: 0 additions & 12 deletions .travis.yml

This file was deleted.

4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
[![Build Status](https://travis-ci.org/cgauge/task-manager-lib.svg?branch=master)](https://travis-ci.org/cgauge/task-manager-lib)
[![Code Coverage](https://scrutinizer-ci.com/g/cgauge/task-manager-lib/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/cgauge/task-manager-lib/?branch=master)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/cgauge/task-manager-lib/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/cgauge/task-manager-lib/?branch=master)

# Task Manager ⚙️

# Installation
Expand Down
13 changes: 9 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
}
],
"require": {
"php": ">=7.4"
"php": ">=8.2"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"doctrine/coding-standard": "^8.0",
"phpstan/phpstan": "^0.12.64"
"phpunit/phpunit": "^10.0",
"doctrine/coding-standard": "^12.0",
"phpstan/phpstan": "^1.0"
},
"autoload": {
"psr-4": {
Expand All @@ -29,5 +29,10 @@
"psr-4": {
"Test\\CustomerGauge\\TaskManager\\": "tests/"
}
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
}
}
18 changes: 18 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0"?>
<ruleset>
<arg name="basepath" value="."/>
<arg name="extensions" value="php"/>
<arg name="parallel" value="80"/>
<arg name="cache" value=".phpcs-cache"/>
<arg name="colors"/>

<!-- Ignore warnings, show progress of the run and show sniff names -->
<arg value="nps"/>

<!-- Directories to be checked -->
<file>src</file>
<file>tests</file>

<!-- Include full Doctrine Coding Standard -->
<rule ref="Doctrine"/>
</ruleset>
29 changes: 14 additions & 15 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./src</directory>
</include>
</coverage>
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.2/phpunit.xsd"
backupGlobals="false"
bootstrap="vendor/autoload.php"
colors="true"
processIsolation="false"
stopOnFailure="false"
cacheDirectory=".phpunit.cache"
backupStaticProperties="false">
<coverage/>
<testsuites>
<testsuite name="Unit Tests">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">./src</directory>
</include>
</source>
</phpunit>
12 changes: 5 additions & 7 deletions src/InvalidTaskAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,19 @@
namespace CustomerGauge\TaskManager;

use InvalidArgumentException;
use function get_class;

use function implode;
use function sprintf;

class InvalidTaskAttribute extends InvalidArgumentException
{
/**
* @param array<int, int|string> $keys
*/
public static function duplicatedKey(Task $task, array $keys) : self
/** @param array<int, int|string> $keys */
public static function duplicatedKey(Task $task, array $keys): self
{
$message = sprintf(
'Duplicate task [%s] attribute keys [%s]. Use a different attribute key.',
get_class($task),
implode(', ', $keys)
$task::class,
implode(', ', $keys),
);

return new static($message);
Expand Down
6 changes: 2 additions & 4 deletions src/Reversible.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@

interface Reversible
{
/**
* @param mixed[] $attributes
*/
public function reverse(array $attributes) : void;
/** @param mixed[] $attributes */
public function reverse(array $attributes): void;
}
14 changes: 5 additions & 9 deletions src/Strategy/ContinueOnFailure.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,10 @@
class ContinueOnFailure implements Strategy
{
/** @var Throwable[] */
private $exceptions = [];
private array $exceptions = [];

/**
* @param mixed[] $context
*/
public function execute(callable $callback, array $context = []) : void
/** @param mixed[] $context */
public function execute(callable $callback, array $context = []): void
{
try {
$callback();
Expand All @@ -42,10 +40,8 @@ public function execute(callable $callback, array $context = []) : void
}
}

/**
* @return Throwable[]
*/
public function exceptions() : array
/** @return Throwable[] */
public function exceptions(): array
{
return $this->exceptions;
}
Expand Down
17 changes: 8 additions & 9 deletions src/Strategy/RollbackOnFailure.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,36 +26,35 @@
use CustomerGauge\TaskManager\Reversible;
use CustomerGauge\TaskManager\Task;
use Throwable;

use function array_filter;
use function array_unshift;

class RollbackOnFailure implements Strategy
{
/** @var Task[] */
private $executed = [];
private array $executed = [];

/** @var mixed[] */
private $context;
private array $context;

/**
* @param mixed[] $context
*/
public function execute(callable $callback, array $context = []) : void
/** @param mixed[] $context */
public function execute(callable $callback, array $context = []): void
{
$this->context = $context;

try {
$task = $callback();

array_unshift($this->executed, $task);
} catch (Throwable $e) {
} catch (Throwable) {
$this->rollback();
}
}

public function rollback() : void
public function rollback(): void
{
$tasks = array_filter($this->executed, static function ($task) : bool {
$tasks = array_filter($this->executed, static function ($task): bool {
return $task instanceof Reversible;
});

Expand Down
6 changes: 2 additions & 4 deletions src/Strategy/StopOnFailure.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@

class StopOnFailure implements Strategy
{
/**
* @param mixed[] $context
*/
public function execute(callable $callback, array $context = []) : void
/** @param mixed[] $context */
public function execute(callable $callback, array $context = []): void
{
$callback();
}
Expand Down
6 changes: 2 additions & 4 deletions src/Strategy/Strategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@

interface Strategy
{
/**
* @param mixed[] $context
*/
public function execute(callable $callback, array $context = []) : void;
/** @param mixed[] $context */
public function execute(callable $callback, array $context = []): void;
}
2 changes: 1 addition & 1 deletion src/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ interface Task
*
* @return mixed[]
*/
public function run(array $attributes) : array;
public function run(array $attributes): array;
}
34 changes: 15 additions & 19 deletions src/TaskManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,28 @@

use CustomerGauge\TaskManager\Strategy\StopOnFailure;
use CustomerGauge\TaskManager\Strategy\Strategy;

use function array_filter;
use function array_intersect_key;
use function array_keys;
use function array_reverse;

class TaskManager implements Task
{
/** @var Task[] */
private $tasks = [];
/** @var Task[]|Reversible[] */
private array $tasks = [];

/** @var mixed[] */
private $attributes = [];
private array $attributes = [];

/** @var Strategy */
private $strategy;
private Strategy $strategy;

public function __construct(?Strategy $strategy = null)
public function __construct(Strategy|null $strategy = null)
{
$this->strategy = $strategy ?? new StopOnFailure();
}

public function add(Task $task) : self
public function add(Task $task): self
{
$this->tasks[] = $task;

Expand All @@ -58,12 +58,12 @@ public function add(Task $task) : self
*
* @return mixed[]
*/
public function run(array $attributes) : array
public function run(array $attributes): array
{
$this->attributes = $attributes;

foreach ($this->tasks as $task) {
$this->strategy->execute(function () use ($task) : Task {
$this->strategy->execute(function () use ($task): Task {
$attributes = $task->run($this->attributes);

$this->checkForDuplicatedKey($task, $attributes);
Expand All @@ -77,28 +77,24 @@ public function run(array $attributes) : array
return $this->attributes;
}

/**
* @param mixed[] $attributes
*/
public function reverse(array $attributes) : void
/** @param mixed[] $attributes */
public function reverse(array $attributes): void
{
$tasks = array_reverse($this->tasks);

$tasks = array_filter($tasks, static function ($task) : bool {
$tasks = array_filter($tasks, static function ($task): bool {
return $task instanceof Reversible;
});

foreach ($this->tasks as $task) {
$this->strategy->execute(function () use ($task, $attributes) : void {
$this->strategy->execute(static function () use ($task, $attributes): void {
$task->reverse($attributes);
}, $attributes);
}
}

/**
* @param mixed[] $attributes
*/
private function checkForDuplicatedKey(Task $task, array $attributes) : void
/** @param mixed[] $attributes */
private function checkForDuplicatedKey(Task $task, array $attributes): void
{
$duplicated = array_intersect_key($this->attributes, $attributes);

Expand Down
Loading

0 comments on commit a4a062f

Please sign in to comment.