Skip to content

Commit

Permalink
composer phpcbf
Browse files Browse the repository at this point in the history
  • Loading branch information
kento-oka committed Jan 31, 2022
1 parent fb26944 commit 64f315e
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 20 deletions.
3 changes: 3 additions & 0 deletions src/AttributeLoader.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

declare(strict_types=1);

namespace Fratily\AttributeLoader;
Expand Down Expand Up @@ -66,6 +67,7 @@ public function __construct(
$this->allowSubClasses = $allowSubClasses;
}

// phpcs:disable Generic.Files.LineLength.TooLong
/**
* Returns an instance of the attribute attached to reflection.
*
Expand All @@ -74,6 +76,7 @@ public function __construct(
*
* @phpstan-return list<T>
*/
// phpcs:enable Generic.Files.LineLength.TooLong
public function load(
ReflectionClass
| ReflectionClassConstant
Expand Down
5 changes: 4 additions & 1 deletion tests/Helper/BarNotAttribute.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php

declare(strict_types=1);

namespace Fratily\Tests\AttributeLoader\Helper;

class BarNotAttribute extends FooAttribute {}
class BarNotAttribute extends FooAttribute
{
}
5 changes: 4 additions & 1 deletion tests/Helper/BazAttribute.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
<?php

declare(strict_types=1);

namespace Fratily\Tests\AttributeLoader\Helper;

use Attribute;

#[Attribute()]
class BazAttribute extends BarNotAttribute {}
class BazAttribute extends BarNotAttribute
{
}
5 changes: 4 additions & 1 deletion tests/Helper/FooAttribute.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
<?php

declare(strict_types=1);

namespace Fratily\Tests\AttributeLoader\Helper;

use Attribute;

#[Attribute()]
class FooAttribute {}
class FooAttribute
{
}
7 changes: 4 additions & 3 deletions tests/Unit/ConstructTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

declare(strict_types=1);

namespace Fratily\Tests\AttributeLoader\Unit;
Expand All @@ -11,13 +12,13 @@
class ConstructTest extends TestCase
{
/**
* @dataProvider dataProvider_invalidParameters
* @dataProvider dataProviderInvalidParameters
*
* @template T of object
* @phpstan-param class-string<T> $attributeClass
* @phpstan-param (callable(\ReflectionAttribute<T>):T)|null $attributeInstanceBuilder
*/
public function test_invalidParameters(
public function testInvalidParameters(
string $exceptionMessage,
string $attributeClass,
callable|null $attributeInstanceBuilder,
Expand All @@ -32,7 +33,7 @@ public function test_invalidParameters(
/**
* @phpstan-return array<string,array{string,class-string,callable|null,bool}>
*/
public function dataProvider_invalidParameters(): array
public function dataProviderInvalidParameters(): array
{
/** @phpstan-var class-string */
$not_exists_class = 'not_exists_class';
Expand Down
23 changes: 15 additions & 8 deletions tests/Unit/LoadTest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
<?php

declare(strict_types=1);

namespace Fratily\Tests\AttributeLoader\Unit;

// phpcs:disable -- PSR1.Files.SideEffects.FoundWithSymbols
include __DIR__ . '/load_test_helper.php';
// phpcs:enable

use Error;
use Fratily\AttributeLoader\AttributeLoader;
Expand All @@ -16,7 +19,7 @@

class LoadTest extends TestCase
{
public function test_subclassWillNotBeDetectedIfAllowSubClassIsNone(): void
public function testSubclassWillNotBeDetectedIfAllowSubClassIsNone(): void
{
$attributes = (new AttributeLoader(FooAttribute::class))->load(
new ReflectionFunction('func_baz_and_foo_attribute')
Expand All @@ -26,7 +29,7 @@ public function test_subclassWillNotBeDetectedIfAllowSubClassIsNone(): void
$this->assertSame(FooAttribute::class, get_class($attributes[0]));
}

public function test_subclassWillNotBeDetectedIfAllowSubClassIsFalse(): void
public function testSubclassWillNotBeDetectedIfAllowSubClassIsFalse(): void
{
$attributes = (new AttributeLoader(FooAttribute::class, null, false))->load(
new ReflectionFunction('func_baz_and_foo_attribute')
Expand All @@ -36,7 +39,7 @@ public function test_subclassWillNotBeDetectedIfAllowSubClassIsFalse(): void
$this->assertSame(FooAttribute::class, get_class($attributes[0]));
}

public function test_subclassWillBeDetectedIfAllowSubClassIsTrue(): void
public function testSubclassWillBeDetectedIfAllowSubClassIsTrue(): void
{
$attributes = (new AttributeLoader(FooAttribute::class, null, true))->load(
new ReflectionFunction('func_baz_and_foo_attribute')
Expand All @@ -48,11 +51,11 @@ public function test_subclassWillBeDetectedIfAllowSubClassIsTrue(): void
}

/**
* @dataProvider dataProvider_invalidBuilder
* @dataProvider dataProviderInvalidBuilder
* @phpstan-param callable(\ReflectionAttribute<FooAttribute>):FooAttribute $builder
* @phpstan-param class-string<\Throwable> $exception
*/
public function test_invalidBuilder(
public function testInvalidBuilder(
callable $builder,
bool $allow_sub_class,
string $exception,
Expand All @@ -65,10 +68,12 @@ public function test_invalidBuilder(
new ReflectionFunction('func_foo_attribute')
);
}
// phpcs:disable Generic.Files.LineLength.TooLong
/**
* @phpstan-return array<string,array{callable(\ReflectionAttribute<\Attribute>):mixed,bool,class-string<\Throwable>,string}>
*/
public function dataProvider_invalidBuilder(): array
// phpcs:enable Generic.Files.LineLength.TooLong
public function dataProviderInvalidBuilder(): array
{
return [
'returned not object' => [
Expand All @@ -82,13 +87,15 @@ public function dataProvider_invalidBuilder(): array
fn() => new BazAttribute(),
true,
LogicException::class,
// phpcs:disable Generic.Files.LineLength.TooLong
'The builder must return an instance of the specified attribute class.'
. ' Expected instance of ' . FooAttribute::class . ', but instance of ' . BazAttribute::class . ' was returned.'
// phpcs:enable Generic.Files.LineLength.TooLong
],
];
}

public function test_notAttributeClassCannotMakeInstanceIfWithoutBuilder(): void
public function testNotAttributeClassCannotMakeInstanceIfWithoutBuilder(): void
{
$this->expectException(Error::class);
$this->expectExceptionMessage(
Expand All @@ -100,7 +107,7 @@ public function test_notAttributeClassCannotMakeInstanceIfWithoutBuilder(): void
);
}

public function test_notAttributeClassCanMakeInstanceIfWithBuilder(): void
public function testNotAttributeClassCanMakeInstanceIfWithBuilder(): void
{
$attributes = (new AttributeLoader(FooAttribute::class, fn() => new BarNotAttribute(), true))->load(
new ReflectionFunction('func_bar_not_attribute')
Expand Down
19 changes: 13 additions & 6 deletions tests/Unit/load_test_helper.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
<?php
declare(strict_types=1);

use Fratily\Tests\AttributeLoader\Unit\LoadTest;
declare(strict_types=1);

use Fratily\Tests\AttributeLoader\Helper\FooAttribute;
use Fratily\Tests\AttributeLoader\Helper\BarNotAttribute;
use Fratily\Tests\AttributeLoader\Helper\BazAttribute;

#[FooAttribute]
function func_foo_attribute(): void {}
function func_foo_attribute(): void
{
}

// @phpstan-ignore-next-line not an Attribute class.
#[BarNotAttribute]
function func_bar_not_attribute(): void {}
function func_bar_not_attribute(): void
{
}

#[BazAttribute]
function func_baz_attribute(): void {}
function func_baz_attribute(): void
{
}

#[BazAttribute, FooAttribute]
function func_baz_and_foo_attribute(): void {}
function func_baz_and_foo_attribute(): void
{
}

0 comments on commit 64f315e

Please sign in to comment.