mirror of
https://github.com/pestphp/pest.git
synced 2026-03-06 07:47:22 +01:00
Merge pull request #1279 from midnite81/bug/declare-strict-types-with-comments-above
Strict types expectation allows for comments above declaration
This commit is contained in:
@ -509,7 +509,7 @@ final class Expectation
|
|||||||
{
|
{
|
||||||
return Targeted::make(
|
return Targeted::make(
|
||||||
$this,
|
$this,
|
||||||
fn (ObjectDescription $object): bool => (bool) preg_match('/^<\?php\s+declare\(.*?strict_types\s?=\s?1.*?\);/', (string) file_get_contents($object->path)),
|
fn (ObjectDescription $object): bool => (bool) preg_match('/^<\?php\s*(\/\*[\s\S]*?\*\/|\/\/[^\r\n]*(?:\r?\n|$)|\s)*declare\s*\(\s*strict_types\s*=\s*1\s*\)\s*;/m', (string) file_get_contents($object->path)),
|
||||||
'to use strict types',
|
'to use strict types',
|
||||||
FileLineFinder::where(fn (string $line): bool => str_contains($line, '<?php')),
|
FileLineFinder::where(fn (string $line): bool => str_contains($line, '<?php')),
|
||||||
);
|
);
|
||||||
|
|||||||
18
tests/Features/Expect/toUseStrictTypes.php
Normal file
18
tests/Features/Expect/toUseStrictTypes.php
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
use Pest\Arch\Exceptions\ArchExpectationFailedException;
|
||||||
|
use Tests\Fixtures\Arch\ToUseStrictTypes\HasNoStrictType;
|
||||||
|
use Tests\Fixtures\Arch\ToUseStrictTypes\HasStrictType;
|
||||||
|
use Tests\Fixtures\Arch\ToUseStrictTypes\HasStrictTypeWithCommentsAbove;
|
||||||
|
|
||||||
|
test('pass', function () {
|
||||||
|
expect(HasStrictType::class)->toUseStrictTypes()
|
||||||
|
->and(HasStrictTypeWithCommentsAbove::class)->toUseStrictTypes();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('failures', function () {
|
||||||
|
expect(HasNoStrictType::class)->toUseStrictTypes();
|
||||||
|
})->throws(ArchExpectationFailedException::class);
|
||||||
|
|
||||||
8
tests/Fixtures/Arch/ToUseStrictTypes/HasNoStrictType.php
Normal file
8
tests/Fixtures/Arch/ToUseStrictTypes/HasNoStrictType.php
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Fixtures\Arch\ToUseStrictTypes;
|
||||||
|
|
||||||
|
class HasNoStrictType
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
10
tests/Fixtures/Arch/ToUseStrictTypes/HasStrictType.php
Normal file
10
tests/Fixtures/Arch/ToUseStrictTypes/HasStrictType.php
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Tests\Fixtures\Arch\ToUseStrictTypes;
|
||||||
|
|
||||||
|
class HasStrictType
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,12 @@
|
|||||||
|
<?php /** @noinspection PhpUnused */
|
||||||
|
|
||||||
|
// some other comment
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Tests\Fixtures\Arch\ToUseStrictTypes;
|
||||||
|
|
||||||
|
class HasStrictTypeWithCommentsAbove
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user