generated from pestphp/pest-plugin-template
-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from fabio-ivona/fix-builtin-functions-not-dete…
…cted [fix] core expressions not detected
- Loading branch information
Showing
11 changed files
with
234 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"notPath": [ | ||
"tests/Fixtures/Misc/HasNativeFunctions.php" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Pest\Arch\Objects; | ||
|
||
use Pest\Arch\Support\PhpCoreExpressions; | ||
use PhpParser\Node\Expr; | ||
use PHPUnit\Architecture\Asserts\Dependencies\Elements\ObjectUses; | ||
use PHPUnit\Architecture\Services\ServiceContainer; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class ObjectDescription extends \PHPUnit\Architecture\Elements\ObjectDescription // @phpstan-ignore-line | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public static function make(string $path): ?self | ||
{ | ||
/** @var ObjectDescription|null $description */ | ||
$description = parent::make($path); | ||
|
||
if (! $description instanceof \Pest\Arch\Objects\ObjectDescription) { | ||
return null; | ||
} | ||
|
||
$description->uses = new ObjectUses( | ||
[ | ||
...$description->uses->getIterator(), | ||
...self::retrieveCoreUses($description), | ||
] | ||
); | ||
|
||
return $description; | ||
} | ||
|
||
/** | ||
* @return array<int, string> | ||
*/ | ||
private static function retrieveCoreUses(ObjectDescription $description): array | ||
{ | ||
|
||
$expressions = []; | ||
|
||
foreach (PhpCoreExpressions::$ENABLED as $expression) { | ||
$expressions = [ | ||
...$expressions, | ||
...ServiceContainer::$nodeFinder->findInstanceOf( | ||
$description->stmts, | ||
$expression, | ||
), | ||
]; | ||
} | ||
|
||
/** @var array<int, Expr> $expressions */ | ||
return array_filter(array_map(fn (Expr $expression): string => PhpCoreExpressions::getName($expression), $expressions)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Pest\Arch\Support; | ||
|
||
use Pest\Exceptions\ShouldNotHappen; | ||
use PhpParser\Node\Expr; | ||
use PhpParser\Node\Expr\Clone_; | ||
use PhpParser\Node\Expr\Empty_; | ||
use PhpParser\Node\Expr\Eval_; | ||
use PhpParser\Node\Expr\Exit_; | ||
use PhpParser\Node\Expr\Include_; | ||
use PhpParser\Node\Expr\Isset_; | ||
use PhpParser\Node\Expr\Print_; | ||
use PhpParser\Node\Expr\ShellExec; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class PhpCoreExpressions | ||
{ | ||
/** | ||
* @var array<int, class-string<Expr>> | ||
*/ | ||
public static array $ENABLED = [ | ||
Clone_::class, | ||
Empty_::class, | ||
Eval_::class, | ||
Exit_::class, | ||
Isset_::class, | ||
Print_::class, | ||
]; | ||
|
||
public static function getName(Expr $expression): string | ||
{ | ||
return match ($expression::class) { | ||
Clone_::class => 'clone', | ||
Empty_::class => 'empty', | ||
Eval_::class => 'eval', | ||
Exit_::class => match ($expression->getAttribute('kind')) { | ||
Exit_::KIND_EXIT => 'exit', | ||
Exit_::KIND_DIE => 'die', | ||
default => throw ShouldNotHappen::fromMessage('Unhandled Exit expression kind '.$expression->getAttribute('kind')), | ||
}, | ||
Isset_::class => 'isset', | ||
Print_::class => 'print', | ||
default => throw ShouldNotHappen::fromMessage('Unsupported Core Expression'), | ||
}; | ||
} | ||
|
||
public static function getClass(string $name): ?string | ||
{ | ||
return match ($name) { | ||
'clone' => Clone_::class, | ||
'empty' => Empty_::class, | ||
'eval' => Eval_::class, | ||
'die', 'exit' => Exit_::class, | ||
'include' => Include_::class, | ||
'isset' => Isset_::class, | ||
'print' => Print_::class, | ||
'shell_exec' => ShellExec::class, | ||
default => null, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
namespace Tests\Fixtures\Misc; | ||
|
||
class HasNativeFunctions | ||
{ | ||
public function startSleeping(): void | ||
{ | ||
sleep(1); | ||
} | ||
|
||
public function dieWithStatus(int $status): void | ||
{ | ||
die($status); | ||
} | ||
|
||
public function exitWithStatus(int $status): void | ||
{ | ||
exit($status); | ||
} | ||
|
||
public function evaluateCode(string $code): void | ||
{ | ||
eval($code); | ||
} | ||
|
||
public function makeAClone(object $object): object | ||
{ | ||
return clone $object; | ||
} | ||
|
||
public function checkArray(array $array): bool | ||
{ | ||
if(empty($array)){ | ||
return false; | ||
} | ||
|
||
if(!isset($array[1])){ | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public function printSomething(string $text): void | ||
{ | ||
print $text; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.