diff --git a/.gitattributes b/.gitattributes index 9365305..89decca 100644 --- a/.gitattributes +++ b/.gitattributes @@ -4,7 +4,7 @@ /testo.php export-ignore /tests export-ignore /.editorconfig export-ignore -/.php_cs.dist.php export-ignore +/.php-cs-fixer.dist.php export-ignore /psalm.xml export-ignore /psalm.xml.dist export-ignore /UPGRADING.md export-ignore diff --git a/.github/workflows/cs-fix.yml b/.github/workflows/cs-fix.yml new file mode 100644 index 0000000..14f585c --- /dev/null +++ b/.github/workflows/cs-fix.yml @@ -0,0 +1,17 @@ +--- + +on: # yamllint disable-line rule:truthy + push: + branches: + - '*' + +name: Fix Code Style + +jobs: + cs-fix: + permissions: + contents: write + uses: spiral/gh-actions/.github/workflows/cs-fix.yml@master + with: + php: >- + ['8.4'] diff --git a/.gitignore b/.gitignore index 78d21b7..ea10c32 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ docs vendor node_modules .php-cs-fixer.cache +runtime diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php new file mode 100644 index 0000000..45745a1 --- /dev/null +++ b/.php-cs-fixer.dist.php @@ -0,0 +1,13 @@ +include(__DIR__ . '/src') + ->include(__DIR__ . '/tests') + ->include(__FILE__) + ->cache('./runtime/php-cs-fixer.cache') + ->allowRisky(false) + ->build(); diff --git a/.styleci.yml b/.styleci.yml deleted file mode 100644 index 6fde134..0000000 --- a/.styleci.yml +++ /dev/null @@ -1,73 +0,0 @@ -preset: psr12 -risky: true - -version: 8 - -enabled: - - alpha_ordered_traits - - array_indentation - - array_push - - combine_consecutive_issets - - combine_consecutive_unsets - - combine_nested_dirname - - declare_strict_types - - dir_constant - - fully_qualified_strict_types - - function_to_constant - - is_null - - magic_constant_casing - - magic_method_casing - - method_separation - - modernize_types_casting - - native_function_casing - - native_function_type_declaration_casing - - no_alias_functions - - no_empty_comment - - no_empty_phpdoc - - no_empty_statement - - no_extra_block_blank_lines - - no_short_bool_cast - - no_superfluous_elseif - - no_unneeded_control_parentheses - - no_unneeded_curly_braces - - no_unneeded_final_method - - no_unset_cast - - no_unused_imports - - no_unused_lambda_imports - - no_useless_else - - no_useless_return - - normalize_index_brace - - php_unit_dedicate_assert - - php_unit_dedicate_assert_internal_type - - php_unit_expectation - - php_unit_mock - - php_unit_mock_short_will_return - - php_unit_namespaced - - php_unit_no_expectation_annotation - - phpdoc_no_empty_return - - phpdoc_no_useless_inheritdoc - - phpdoc_order - - phpdoc_property - - phpdoc_scalar - - phpdoc_separation - - phpdoc_singular_inheritdoc - - phpdoc_trim - - phpdoc_trim_consecutive_blank_line_separation - - phpdoc_type_to_var - - phpdoc_types - - phpdoc_types_order - - print_to_echo - - regular_callable_call - - return_assignment - - self_accessor - - self_static_accessor - - set_type_to_cast - - short_array_syntax - - short_list_syntax - - simplified_if_return - - single_quote - - standardize_not_equals - - ternary_to_null_coalescing - - trailing_comma_in_multiline_array - - unalign_double_arrow - - unalign_equals diff --git a/composer.json b/composer.json index 1a8b7da..4c773fd 100644 --- a/composer.json +++ b/composer.json @@ -51,6 +51,7 @@ }, "require-dev": { "mockery/mockery": "^1.6", + "spiral/code-style": "^2.3.1", "testo/bridge-mockery": "^0.1.2", "testo/testo": "^0.10.55", "vimeo/psalm": "^6 || ^7" @@ -67,6 +68,8 @@ }, "scripts": { "test": "vendor/bin/testo", + "cs:diff": "php-cs-fixer fix --dry-run -v --diff", + "cs:fix": "php-cs-fixer fix -v", "psalm": "vendor/bin/psalm --config=psalm.xml ./src --show-info=true" }, "config": { diff --git a/src/Lock.php b/src/Lock.php index 2ef70d7..bd03f6c 100644 --- a/src/Lock.php +++ b/src/Lock.php @@ -4,7 +4,6 @@ namespace RoadRunner\Lock; -use DateInterval; use RoadRunner\Lock\DTO\V1BETA1\{ Request, Response }; @@ -31,8 +30,8 @@ public function __construct( * * @param non-empty-string $resource The name of the resource to be locked. * @param non-empty-string|null $id The lock ID. If not specified, a random UUID will be generated. - * @param int|float|DateInterval $ttl The time-to-live of the lock, in seconds. Defaults to 0 (forever). - * @param int|float|DateInterval $waitTTL How long to wait for the lock to become free before giving up, in seconds. + * @param int|float|\DateInterval $ttl The time-to-live of the lock, in seconds. Defaults to 0 (forever). + * @param int|float|\DateInterval $waitTTL How long to wait for the lock to become free before giving up, in seconds. * Defaults to 0. With 0 the call is effectively non-blocking: the RoadRunner * server caps the acquire window at defaultImmediateTimeout (1ms), so false * is returned almost immediately when the resource is already locked. A @@ -45,8 +44,8 @@ public function __construct( public function lock( string $resource, ?string $id = null, - int|float|DateInterval $ttl = 0, - int|float|DateInterval $waitTTL = 0, + int|float|\DateInterval $ttl = 0, + int|float|\DateInterval $waitTTL = 0, ): false|string { $request = new Request(); $request->setResource($resource); @@ -68,8 +67,8 @@ public function lock( * * @param non-empty-string $resource The name of the resource to be locked. * @param non-empty-string|null $id The lock ID. If not specified, a random UUID will be generated. - * @param int|float|DateInterval $ttl The time-to-live of the lock, in seconds. Defaults to 0 (forever). - * @param int|float|DateInterval $waitTTL How long to wait for the lock to become free before giving up, in seconds. + * @param int|float|\DateInterval $ttl The time-to-live of the lock, in seconds. Defaults to 0 (forever). + * @param int|float|\DateInterval $waitTTL How long to wait for the lock to become free before giving up, in seconds. * Defaults to 0. With 0 the call is effectively non-blocking: the RoadRunner * server caps the acquire window at defaultImmediateTimeout (1ms), so false * is returned almost immediately when the resource is already locked. A @@ -82,8 +81,8 @@ public function lock( public function lockRead( string $resource, ?string $id = null, - int|float|DateInterval $ttl = 0, - int|float|DateInterval $waitTTL = 0, + int|float|\DateInterval $ttl = 0, + int|float|\DateInterval $waitTTL = 0, ): false|string { $request = new Request(); $request->setResource($resource); @@ -162,12 +161,12 @@ public function exists(string $resource, ?string $id = null): bool * * @param string $resource The name of the resource to update the TTL for. * @param string $id Lock ID from lock or lockRead method. - * @param int|float|DateInterval $ttl The new TTL in seconds. + * @param int|float|\DateInterval $ttl The new TTL in seconds. * @return bool Returns true on success and false on failure. * * @throws \InvalidArgumentException If ttl is negative. */ - public function updateTTL(string $resource, string $id, int|float|DateInterval $ttl): bool + public function updateTTL(string $resource, string $id, int|float|\DateInterval $ttl): bool { $request = new Request(); $request->setResource($resource); @@ -179,10 +178,10 @@ public function updateTTL(string $resource, string $id, int|float|DateInterval $ return $response->getOk(); } - private function convertTimeToMicroseconds(int|float|DateInterval $ttl): int + private function convertTimeToMicroseconds(int|float|\DateInterval $ttl): int { - if ($ttl instanceof DateInterval) { - return (int) \round((int)$ttl->format('%s') * 1_000_000); + if ($ttl instanceof \DateInterval) { + return (int) \round((int) $ttl->format('%s') * 1_000_000); } \assert($ttl >= 0, 'TTL must be positive'); @@ -194,8 +193,6 @@ private function convertTimeToMicroseconds(int|float|DateInterval $ttl): int * Make an RPC call to the RoadRunner server. * * @param non-empty-string $method - * @param Request $request - * @return Response */ private function call(string $method, Request $request): Response { diff --git a/src/LockIdGeneratorInterface.php b/src/LockIdGeneratorInterface.php index 90be2c6..8b45294 100644 --- a/src/LockIdGeneratorInterface.php +++ b/src/LockIdGeneratorInterface.php @@ -15,4 +15,4 @@ interface LockIdGeneratorInterface * @return non-empty-string */ public function generate(): string; -} \ No newline at end of file +} diff --git a/src/UuidLockIdGenerator.php b/src/UuidLockIdGenerator.php index dae90e3..8ee41f7 100644 --- a/src/UuidLockIdGenerator.php +++ b/src/UuidLockIdGenerator.php @@ -11,11 +11,10 @@ final class UuidLockIdGenerator implements LockIdGeneratorInterface { public function __construct( private readonly UuidFactoryInterface $factory = new UuidFactory(), - ) { - } + ) {} public function generate(): string { return $this->factory->uuid4()->toString(); } -} \ No newline at end of file +} diff --git a/tests/src/LockTest.php b/tests/src/LockTest.php index c8b046c..3ae872d 100644 --- a/tests/src/LockTest.php +++ b/tests/src/LockTest.php @@ -25,18 +25,58 @@ final class LockTest private LockIdGeneratorInterface|MockInterface $idGenerator; private Lock $lock; - #[BeforeTest] - protected function setUp(): void + public static function lockTypeDataProvider(): \Generator { - $this->rpc = m::mock(RPCInterface::class); + foreach (self::lockDataProvider() as $name => $data) { + foreach ([true, false] as $result) { + foreach (['id1', null] as $id) { + yield 'lock: ' . $name . ' | ' . $id . ' | ' . \var_export($result, true) => [ + 'lock', + 'lock.Lock', + ...$data, + $result, + $id, + ]; - $this->rpc->shouldReceive('withCodec') - ->with(m::type(ProtobufCodec::class)) - ->once() - ->andReturnSelf(); + yield 'read-lock: ' . $name . ' | ' . $id . ' | ' . \var_export($result, true) => [ + 'lockRead', + 'lock.LockRead', + ...$data, + $result, + $id, + ]; + } + } + } + } - $this->idGenerator = m::mock(LockIdGeneratorInterface::class); - $this->lock = new Lock($this->rpc, $this->idGenerator); + public static function updateTTLDataProvider(): \Traversable + { + foreach (self::lockDataProvider() as $name => $data) { + foreach ([true, false] as $result) { + yield $name . ' | ' . \var_export($result, true) => [$data[0], $data[1], $result]; + } + } + } + + public static function lockDataProvider(): \Generator + { + yield 'int' => [10, 10_000_000, 8, 8_000_000,]; + + yield 'float' => [0.000_01, 10, 0.000_004, 4,]; + + yield 'date-interval' => [ + new \DateInterval('PT10S'), + 10_000_000, + new \DateInterval('PT9S'), + 9_000_000, + ]; + } + + public static function resultDataProvider(): \Traversable + { + yield [true]; + yield [false]; } #[DataProvider('lockTypeDataProvider')] @@ -159,60 +199,6 @@ public function testExistsWithId(bool $result): void Assert::same($this->lock->exists('resource', 'some-id'), $result); } - public static function lockTypeDataProvider(): \Generator - { - foreach (self::lockDataProvider() as $name => $data) { - foreach ([true, false] as $result) { - foreach (['id1', null] as $id) { - yield 'lock: ' . $name . ' | ' .$id. ' | ' . \var_export($result, true) => [ - 'lock', - 'lock.Lock', - ...$data, - $result, - $id - ]; - - yield 'read-lock: ' . $name . ' | ' .$id. ' | ' . \var_export($result, true) => [ - 'lockRead', - 'lock.LockRead', - ...$data, - $result, - $id - ]; - } - } - } - } - - public static function updateTTLDataProvider(): \Traversable - { - foreach (self::lockDataProvider() as $name => $data) { - foreach ([true, false] as $result) { - yield $name . ' | ' . \var_export($result, true) => [$data[0], $data[1], $result]; - } - } - } - - public static function lockDataProvider(): \Generator - { - yield 'int' => [10, 10_000_000, 8, 8_000_000,]; - - yield 'float' => [0.000_01, 10, 0.000_004, 4,]; - - yield 'date-interval' => [ - new \DateInterval('PT10S'), - 10_000_000, - new \DateInterval('PT9S'), - 9_000_000, - ]; - } - - public static function resultDataProvider(): \Traversable - { - yield [true]; - yield [false]; - } - #[ExpectException(\LogicException::class)] public function testLockNegativeTtl(): void { @@ -242,4 +228,18 @@ public function testUpdateNegativeWaitTtl(): void { $this->lock->updateTTL('resource', 'uuid', -300); } + + #[BeforeTest] + protected function setUp(): void + { + $this->rpc = m::mock(RPCInterface::class); + + $this->rpc->shouldReceive('withCodec') + ->with(m::type(ProtobufCodec::class)) + ->once() + ->andReturnSelf(); + + $this->idGenerator = m::mock(LockIdGeneratorInterface::class); + $this->lock = new Lock($this->rpc, $this->idGenerator); + } }