Skip to content

Commit c2c8dbb

Browse files
Implement #[WithoutErrorHandler] attribute for #5428
1 parent 41c9726 commit c2c8dbb

File tree

9 files changed

+230
-0
lines changed

9 files changed

+230
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <sebastian@phpunit.de>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace PHPUnit\Framework\Attributes;
11+
12+
use Attribute;
13+
14+
/**
15+
* @psalm-immutable
16+
*
17+
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
18+
*/
19+
#[Attribute(Attribute::TARGET_METHOD)]
20+
final class WithoutErrorHandler
21+
{
22+
}

src/Metadata/Metadata.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,11 @@ public static function usesDefaultClass(string $className): UsesDefaultClass
364364
return new UsesDefaultClass(self::CLASS_LEVEL, $className);
365365
}
366366

367+
public static function withoutErrorHandler(): WithoutErrorHandler
368+
{
369+
return new WithoutErrorHandler(self::METHOD_LEVEL);
370+
}
371+
367372
protected function __construct(int $level)
368373
{
369374
$this->level = $level;
@@ -708,4 +713,12 @@ public function isUsesFunction(): bool
708713
{
709714
return false;
710715
}
716+
717+
/**
718+
* @psalm-assert-if-true WithoutErrorHandler $this
719+
*/
720+
public function isWithoutErrorHandler(): bool
721+
{
722+
return false;
723+
}
711724
}

src/Metadata/MetadataCollection.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,4 +529,14 @@ public function isUsesFunction(): self
529529
),
530530
);
531531
}
532+
533+
public function isWithoutErrorHandler(): self
534+
{
535+
return new self(
536+
...array_filter(
537+
$this->metadata,
538+
static fn (Metadata $metadata): bool => $metadata->isWithoutErrorHandler(),
539+
),
540+
);
541+
}
532542
}

src/Metadata/Parser/AttributeParser.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
use PHPUnit\Framework\Attributes\Ticket;
6565
use PHPUnit\Framework\Attributes\UsesClass;
6666
use PHPUnit\Framework\Attributes\UsesFunction;
67+
use PHPUnit\Framework\Attributes\WithoutErrorHandler;
6768
use PHPUnit\Metadata\Metadata;
6869
use PHPUnit\Metadata\MetadataCollection;
6970
use PHPUnit\Metadata\Version\ConstraintRequirement;
@@ -614,6 +615,13 @@ public function forMethod(string $className, string $methodName): MetadataCollec
614615

615616
$result[] = Metadata::groupOnMethod($attributeInstance->text());
616617

618+
break;
619+
620+
case WithoutErrorHandler::class:
621+
assert($attributeInstance instanceof WithoutErrorHandler);
622+
623+
$result[] = Metadata::withoutErrorHandler();
624+
617625
break;
618626
}
619627
}

src/Metadata/WithoutErrorHandler.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <sebastian@phpunit.de>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace PHPUnit\Metadata;
11+
12+
/**
13+
* @psalm-immutable
14+
*
15+
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
16+
*/
17+
final class WithoutErrorHandler extends Metadata
18+
{
19+
public function isWithoutErrorHandler(): bool
20+
{
21+
return true;
22+
}
23+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <sebastian@phpunit.de>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace PHPUnit\TestFixture\Metadata\Attribute;
11+
12+
use PHPUnit\Framework\Attributes\WithoutErrorHandler;
13+
use PHPUnit\Framework\TestCase;
14+
15+
final class WithoutErrorHandlerTest extends TestCase
16+
{
17+
#[WithoutErrorHandler]
18+
public function testOne(): void
19+
{
20+
}
21+
}

tests/unit/Metadata/MetadataCollectionTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,14 @@ public function test_Can_be_filtered_for_UsesFunction(): void
475475
$this->assertTrue($collection->asArray()[0]->isUsesFunction());
476476
}
477477

478+
public function test_Can_be_filtered_for_WithoutErrorHandler(): void
479+
{
480+
$collection = $this->collectionWithOneOfEach()->isWithoutErrorHandler();
481+
482+
$this->assertCount(1, $collection);
483+
$this->assertTrue($collection->asArray()[0]->isWithoutErrorHandler());
484+
}
485+
478486
private function collectionWithOneOfEach(): MetadataCollection
479487
{
480488
return MetadataCollection::fromArray(
@@ -531,6 +539,7 @@ private function collectionWithOneOfEach(): MetadataCollection
531539
Metadata::usesClass(''),
532540
Metadata::usesDefaultClass(''),
533541
Metadata::usesFunction(''),
542+
Metadata::withoutErrorHandler(),
534543
],
535544
);
536545
}

0 commit comments

Comments
 (0)