Skip to content

Commit

Permalink
Merge pull request #48 from yiisoft/add-rector
Browse files Browse the repository at this point in the history
Add rector [batch]
  • Loading branch information
xepozz committed Oct 28, 2022
2 parents f6bb2fe + 54e0a56 commit 6c0eda6
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 70 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/rector.yml
@@ -0,0 +1,21 @@
on:
pull_request:
paths-ignore:
- 'docs/**'
- 'README.md'
- 'CHANGELOG.md'
- '.gitignore'
- '.gitattributes'
- 'infection.json.dist'
- 'psalm.xml'

name: rector

jobs:
rector:
uses: yiisoft/actions/.github/workflows/rector.yml@master
with:
os: >-
['ubuntu-latest']
php: >-
['8.0']
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -26,6 +26,7 @@
"require-dev": {
"php-mock/php-mock-phpunit": "^2.6",
"phpunit/phpunit": "^9.5",
"rector/rector": "^0.14.3",
"roave/infection-static-analysis-plugin": "^1.16",
"spatie/phpunit-watcher": "^1.23",
"vimeo/psalm": "^4.18",
Expand Down
22 changes: 22 additions & 0 deletions rector.php
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

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

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

// register a single rule
$rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class);

// define sets of rules
$rectorConfig->sets([
LevelSetList::UP_TO_PHP_80,
]);
};
25 changes: 3 additions & 22 deletions src/FileCache.php
Expand Up @@ -28,7 +28,6 @@
use function readdir;
use function rmdir;
use function serialize;
use function strncmp;
use function strpbrk;
use function substr;
use function unlink;
Expand All @@ -49,7 +48,7 @@
*/
final class FileCache implements CacheInterface
{
private const TTL_INFINITY = 31536000; // 1 year
private const TTL_INFINITY = 31_536_000; // 1 year
private const EXPIRATION_EXPIRED = -1;

/**
Expand Down Expand Up @@ -223,8 +222,6 @@ public function has(string $key): bool

/**
* @param string $fileSuffix The cache file suffix. Defaults to '.bin'.
*
* @return self
*/
public function withFileSuffix(string $fileSuffix): self
{
Expand All @@ -237,8 +234,6 @@ public function withFileSuffix(string $fileSuffix): self
* @param int $fileMode The permission to be set for newly created cache files.
* This value will be used by PHP chmod() function. No umask will be applied.
* If not set, the permission will be determined by the current environment.
*
* @return self
*/
public function withFileMode(int $fileMode): self
{
Expand All @@ -251,8 +246,6 @@ public function withFileMode(int $fileMode): self
* @param int $directoryMode The permission to be set for newly created directories.
* This value will be used by PHP chmod() function. No umask will be applied.
* Defaults to 0775, meaning the directory is read-writable by owner and group, but read-only for other users.
*
* @return self
*/
public function withDirectoryMode(int $directoryMode): self
{
Expand All @@ -266,8 +259,6 @@ public function withDirectoryMode(int $directoryMode): self
* If the system has huge number of cache files (e.g. one million), you may use a bigger value
* (usually no bigger than 3). Using sub-directories is mainly to ensure the file system
* is not over burdened with a single directory having too many files.
*
* @return self
*/
public function withDirectoryLevel(int $directoryLevel): self
{
Expand All @@ -280,8 +271,6 @@ public function withDirectoryLevel(int $directoryLevel): self
* @param int $gcProbability The probability (parts per million) that garbage collection (GC) should
* be performed when storing a piece of data in the cache. Defaults to 10, meaning 0.001% chance.
* This number should be between 0 and 1000000. A value 0 means no GC will be performed at all.
*
* @return self
*/
public function withGcProbability(int $gcProbability): self
{
Expand All @@ -292,10 +281,6 @@ public function withGcProbability(int $gcProbability): self

/**
* Converts TTL to expiration.
*
* @param DateInterval|int|string|null $ttl
*
* @return int
*/
private function ttlToExpiration(null|int|string|DateInterval $ttl = null): int
{
Expand Down Expand Up @@ -394,7 +379,7 @@ private function removeCacheFiles(string $path, bool $expiredOnly): void
}

while (($file = readdir($handle)) !== false) {
if (strncmp($file, '.', 1) === 0) {
if (str_starts_with($file, '.')) {
continue;
}

Expand Down Expand Up @@ -423,7 +408,7 @@ private function removeCacheFiles(string $path, bool $expiredOnly): void
*/
private function gc(): void
{
if (random_int(0, 1000000) < $this->gcProbability) {
if (random_int(0, 1_000_000) < $this->gcProbability) {
$this->removeCacheFiles($this->cachePath, true);
}
}
Expand All @@ -449,10 +434,6 @@ private function existsAndNotExpired(string $file): bool

/**
* Converts iterable to array. If provided value is not iterable it throws an InvalidArgumentException.
*
* @param iterable $iterable
*
* @return array
*/
private function iterableToArray(iterable $iterable): array
{
Expand Down
47 changes: 12 additions & 35 deletions tests/FileCacheTest.php
Expand Up @@ -174,8 +174,6 @@ public function testClear($key, $value): void
/**
* @dataProvider dataProviderSetMultiple
*
* @param int|null $ttl
*
* @throws InvalidArgumentException
*/
public function testSetMultiple(?int $ttl): void
Expand Down Expand Up @@ -239,12 +237,9 @@ public function testZeroAndNegativeTtl(): void
/**
* @dataProvider dataProviderNormalizeTtl
*
* @param mixed $ttl
* @param mixed $expectedResult
*
* @throws ReflectionException
*/
public function testNormalizeTtl($ttl, $expectedResult): void
public function testNormalizeTtl(mixed $ttl, mixed $expectedResult): void
{
$this->assertSameExceptObject($expectedResult, $this->invokeMethod($this->cache, 'normalizeTtl', [$ttl]));
}
Expand All @@ -271,12 +266,9 @@ public function dataProviderNormalizeTtl(): array
/**
* @dataProvider ttlToExpirationProvider
*
* @param mixed $ttl
* @param mixed $expected
*
* @throws ReflectionException
*/
public function testTtlToExpiration($ttl, $expected): void
public function testTtlToExpiration(mixed $ttl, mixed $expected): void
{
if ($expected === 'calculate_expiration') {
MockHelper::$time = time();
Expand All @@ -285,7 +277,7 @@ public function testTtlToExpiration($ttl, $expected): void

if ($expected === 'calculate_max_expiration') {
MockHelper::$time = time();
$expected = MockHelper::$time + 31536000;
$expected = MockHelper::$time + 31_536_000;
}

$this->assertSameExceptObject($expected, $this->invokeMethod($this->cache, 'ttlToExpiration', [$ttl]));
Expand All @@ -305,9 +297,6 @@ public function ttlToExpirationProvider(): array
/**
* @dataProvider iterableProvider
*
* @param array $array
* @param iterable $iterable
*
* @throws InvalidArgumentException
*/
public function testValuesAsIterable(array $array, iterable $iterable): void
Expand All @@ -324,11 +313,11 @@ public function iterableProvider(): array
['a' => 1, 'b' => 2,],
['a' => 1, 'b' => 2,],
],
'ArrayIterator' => [
\ArrayIterator::class => [
['a' => 1, 'b' => 2,],
new ArrayIterator(['a' => 1, 'b' => 2,]),
],
'IteratorAggregate' => [
\IteratorAggregate::class => [
['a' => 1, 'b' => 2,],
new class () implements IteratorAggregate {
public function getIterator(): ArrayIterator
Expand Down Expand Up @@ -470,7 +459,7 @@ public function testDirectoryLevel(): void

public function testGcProbability(): void
{
$cache = $this->cache->withGcProbability(1000000);
$cache = $this->cache->withGcProbability(1_000_000);

$this->assertInstanceOf(FileCache::class, $cache);
$this->assertNotSame($this->cache, $cache);
Expand Down Expand Up @@ -528,65 +517,53 @@ public function invalidKeyProvider(): array

/**
* @dataProvider invalidKeyProvider
*
* @param mixed $key
*/
public function testGetThrowExceptionForInvalidKey($key): void
public function testGetThrowExceptionForInvalidKey(mixed $key): void
{
$this->expectException(InvalidArgumentException::class);
$this->cache->get($key);
}

/**
* @dataProvider invalidKeyProvider
*
* @param mixed $key
*/
public function testSetThrowExceptionForInvalidKey($key): void
public function testSetThrowExceptionForInvalidKey(mixed $key): void
{
$this->expectException(InvalidArgumentException::class);
$this->cache->set($key, 'value');
}

/**
* @dataProvider invalidKeyProvider
*
* @param mixed $key
*/
public function testDeleteThrowExceptionForInvalidKey($key): void
public function testDeleteThrowExceptionForInvalidKey(mixed $key): void
{
$this->expectException(InvalidArgumentException::class);
$this->cache->delete($key);
}

/**
* @dataProvider invalidKeyProvider
*
* @param mixed $key
*/
public function testGetMultipleThrowExceptionForInvalidKeys($key): void
public function testGetMultipleThrowExceptionForInvalidKeys(mixed $key): void
{
$this->expectException(InvalidArgumentException::class);
$this->cache->getMultiple([$key]);
}

/**
* @dataProvider invalidKeyProvider
*
* @param mixed $key
*/
public function testDeleteMultipleThrowExceptionForInvalidKeys($key): void
public function testDeleteMultipleThrowExceptionForInvalidKeys(mixed $key): void
{
$this->expectException(InvalidArgumentException::class);
$this->cache->deleteMultiple([$key]);
}

/**
* @dataProvider invalidKeyProvider
*
* @param mixed $key
*/
public function testHasThrowExceptionForInvalidKey($key): void
public function testHasThrowExceptionForInvalidKey(mixed $key): void
{
$this->expectException(InvalidArgumentException::class);
$this->cache->has($key);
Expand Down
2 changes: 0 additions & 2 deletions tests/MockHelper.php
Expand Up @@ -6,8 +6,6 @@

/**
* Mock for the time() function
*
* @return int
*/
function time(): int
{
Expand Down
12 changes: 1 addition & 11 deletions tests/TestCase.php
Expand Up @@ -19,9 +19,6 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase
/**
* Invokes a inaccessible method.
*
* @param object $object
* @param string $method
* @param array $args
* @param bool $revoke whether to make method inaccessible after execution
*
* @throws ReflectionException
Expand All @@ -45,12 +42,9 @@ protected function invokeMethod(object $object, string $method, array $args = []
/**
* Sets an inaccessible object property to a designated value.
*
* @param object $object
* @param string $propertyName
* @param mixed $value
* @param bool $revoke whether to make property inaccessible after setting
*/
protected function setInaccessibleProperty(object $object, string $propertyName, $value, bool $revoke = true): void
protected function setInaccessibleProperty(object $object, string $propertyName, mixed $value, bool $revoke = true): void
{
$class = new ReflectionClass($object);

Expand All @@ -70,8 +64,6 @@ protected function setInaccessibleProperty(object $object, string $propertyName,
/**
* Gets an inaccessible object property.
*
* @param object $object
* @param string $propertyName
* @param bool $revoke whether to make property inaccessible after getting
*
* @return mixed
Expand Down Expand Up @@ -130,8 +122,6 @@ public function getDataProviderData(string $keyPrefix = ''): array
/**
* This function configures given cache to match some expectations
*
* @param CacheInterface $cache
*
* @throws InvalidArgumentException
*
* @return CacheInterface
Expand Down

0 comments on commit 6c0eda6

Please sign in to comment.