-
-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Attribute #70
Open
h4kuna
wants to merge
5
commits into
wrk-flow:main
Choose a base branch
from
h4kuna:attribute
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Attribute #70
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,78 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaraStrict\Testing\Actions; | ||
|
||
use Exception; | ||
use Illuminate\Filesystem\Filesystem; | ||
use LaraStrict\Testing\Constants\ComposerConstants; | ||
use LaraStrict\Testing\Contracts\GetBasePathForStubsActionContract; | ||
use LaraStrict\Testing\Services\ComposerJsonDataService; | ||
|
||
final class ComposerAutoloadAbsoluteAction | ||
{ | ||
/** | ||
* @var array<string, string> | ||
*/ | ||
private array $dirs = []; | ||
|
||
public function __construct( | ||
private readonly ComposerJsonDataService $getComposerJsonDataAction, | ||
private readonly Filesystem $filesystem, | ||
GetBasePathForStubsActionContract $getBasePathForStubsAction, | ||
) { | ||
$this->dirs = $this->prepareSourceDirs($getBasePathForStubsAction->execute()); | ||
} | ||
|
||
/** | ||
* @return array<string, string> | ||
*/ | ||
public function execute(?string $path = null): array | ||
{ | ||
if ($path !== null) { | ||
$this->dirs += $this->loadNewComposer($path); | ||
} | ||
|
||
return $this->dirs; | ||
} | ||
|
||
/** | ||
* @return array<string, string> | ||
*/ | ||
private function prepareSourceDirs(string $basePath): array | ||
{ | ||
$data = $this->getComposerJsonDataAction->data($basePath); | ||
$dirs = []; | ||
|
||
foreach ([ComposerConstants::AutoLoad, ComposerConstants::AutoLoadDev] as $section) { | ||
if (isset($data[$section][ComposerConstants::Psr4]) === false || is_array($data[$section][ComposerConstants::Psr4]) === false) { | ||
continue; | ||
} | ||
|
||
foreach ($data[$section][ComposerConstants::Psr4] as $ns => $path) { | ||
$dirs[$ns] = $basePath . DIRECTORY_SEPARATOR . trim((string) $path, '\\/'); | ||
} | ||
} | ||
|
||
return $dirs; | ||
} | ||
|
||
/** | ||
* @return array<string> | ||
*/ | ||
private function loadNewComposer(string $path): array | ||
{ | ||
if ($this->filesystem->isFile($path)) { | ||
$path = $this->filesystem->dirname($path); | ||
} elseif ($this->filesystem->isDirectory($path) === false) { | ||
throw new Exception(sprintf('The path is not dir "%s".', $path)); | ||
} | ||
|
||
while ($this->getComposerJsonDataAction->isExist($path) === false) { | ||
$path = $this->filesystem->dirname($path); | ||
} | ||
|
||
return $this->prepareSourceDirs($path); | ||
} | ||
} |
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,183 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace LaraStrict\Testing\Actions; | ||
|
||
use LaraStrict\Testing\Constants\StubConstants; | ||
use LaraStrict\Testing\Entities\PhpDocEntity; | ||
use LaraStrict\Testing\Enums\PhpType; | ||
use Nette\PhpGenerator\Literal; | ||
use Nette\PhpGenerator\PhpFile; | ||
use Nette\PhpGenerator\PromotedParameter; | ||
use Nette\PhpGenerator\PsrPrinter; | ||
use ReflectionIntersectionType; | ||
use ReflectionMethod; | ||
use ReflectionNamedType; | ||
use ReflectionParameter; | ||
use ReflectionType; | ||
use ReflectionUnionType; | ||
|
||
final class ExpectationFileContentAction | ||
{ | ||
public const HookProperty = '_hook'; | ||
|
||
|
||
public function execute( | ||
PsrPrinter $printer, | ||
string $namespace, | ||
string $className, | ||
ReflectionMethod $method, | ||
PhpDocEntity $phpDoc, | ||
): string | ||
{ | ||
$parameters = $method->getParameters(); | ||
|
||
$file = new PhpFile(); | ||
$file->setStrictTypes(); | ||
|
||
$class = $file | ||
->addNamespace($namespace) | ||
->addClass($className); | ||
|
||
$constructor = $class | ||
->setFinal() | ||
->addMethod('__construct'); | ||
|
||
$returnType = $method->getReturnType(); | ||
if ($returnType !== null && | ||
($returnType instanceof ReflectionNamedType === false || self::canReturnExpectation($returnType)) || | ||
$phpDoc->returnType === PhpType::Mixed) { | ||
$constructorParameter = $constructor | ||
->addPromotedParameter('return') | ||
->setReadOnly(); | ||
|
||
$this->setParameterType($returnType, $constructorParameter); | ||
} | ||
|
||
$parameterTypes = []; | ||
foreach ($parameters as $parameter) { | ||
$constructorParameter = $constructor | ||
->addPromotedParameter($parameter->name) | ||
->setReadOnly(); | ||
|
||
$parameterTypes[] = $this->setParameterType($parameter->getType(), $constructorParameter); | ||
$this->setParameterDefaultValue($parameter, $constructorParameter); | ||
} | ||
$parameterTypes[] = 'self'; | ||
|
||
$constructor | ||
->addPromotedParameter(self::HookProperty) | ||
->setReadOnly() | ||
->setType('\Closure') | ||
->setNullable() | ||
->setDefaultValue(null); | ||
|
||
$constructor->addComment( | ||
sprintf('@param \Closure(%s):void|null $%s', implode(',', $parameterTypes), self::HookProperty) | ||
); | ||
|
||
return $printer->printFile($file); | ||
} | ||
|
||
|
||
private static function canReturnExpectation(ReflectionNamedType $returnType): bool | ||
{ | ||
return $returnType->getName() !== PhpType::Void->value | ||
&& $returnType->getName() !== PhpType::Self->value | ||
&& $returnType->getName() !== PhpType::Static ->value; | ||
} | ||
|
||
|
||
private function setParameterType( | ||
ReflectionType|ReflectionNamedType|ReflectionUnionType|ReflectionIntersectionType|null $type, | ||
PromotedParameter $constructorParameter | ||
): string | ||
{ | ||
$proposedType = ''; | ||
|
||
$allowNull = false; | ||
$mapToName = static function (ReflectionType $type) use (&$allowNull): ?string { | ||
if ($type instanceof ReflectionNamedType) { | ||
$name = $type->getName(); | ||
if ($name === 'null') { | ||
$allowNull = false; | ||
} | ||
|
||
// Fix global namespace | ||
if (class_exists($name)) { | ||
return '\\' . $name; | ||
} | ||
|
||
return $name; | ||
} | ||
|
||
return null; | ||
}; | ||
|
||
// TODO move to separate action and test with unit test | ||
if ($type instanceof ReflectionNamedType) { | ||
$allowNull = $type->allowsNull(); | ||
$proposedType = $type->getName(); | ||
|
||
if (class_exists($proposedType)) { | ||
// Fix global namespace | ||
$proposedType = '\\' . $proposedType; | ||
} | ||
|
||
$constructorParameter->setNullable($type->allowsNull()); | ||
} elseif ($type instanceof ReflectionUnionType) { | ||
$allowNull = $type->allowsNull(); | ||
$proposedType = implode('|', array_filter(array_map($mapToName, $type->getTypes()))); | ||
} elseif ($type instanceof ReflectionIntersectionType) { | ||
$allowNull = $type->allowsNull(); | ||
$proposedType = implode('&', array_filter(array_map($mapToName, $type->getTypes()))); | ||
} | ||
|
||
if ($proposedType === '') { | ||
$proposedType = 'mixed'; | ||
} | ||
|
||
if ($allowNull) { | ||
$constructorParameter->setNullable($allowNull); | ||
} | ||
|
||
// Callable not supported in property | ||
if ($proposedType === 'callable') { | ||
$proposedType = '\Closure'; | ||
} | ||
|
||
$constructorParameter->setType($proposedType); | ||
|
||
return $proposedType; | ||
} | ||
|
||
|
||
private function setParameterDefaultValue( | ||
ReflectionParameter $parameter, | ||
PromotedParameter $constructorParameter | ||
): void | ||
{ | ||
if ($parameter->isDefaultValueAvailable() === false) { | ||
return; | ||
} | ||
|
||
if ($parameter->isDefaultValueConstant()) { | ||
$constant = $parameter->getDefaultValueConstantName(); | ||
// Ensure that constants are from global scope | ||
$constantLiteral = new Literal(StubConstants::NameSpaceSeparator . $constant); | ||
$constructorParameter->setDefaultValue($constantLiteral); | ||
|
||
return; | ||
} | ||
|
||
$defaultValue = $parameter->getDefaultValue(); | ||
|
||
if (is_object($defaultValue)) { | ||
$objectLiteral = new Literal( | ||
'new ' . StubConstants::NameSpaceSeparator . $defaultValue::class . '(/* unknown */)' | ||
); | ||
$constructorParameter->setDefaultValue($objectLiteral); | ||
} else { | ||
$constructorParameter->setDefaultValue($defaultValue); | ||
} | ||
} | ||
} |
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,86 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace LaraStrict\Testing\Actions; | ||
|
||
use LaraStrict\Testing\Entities\PhpDocEntity; | ||
use LaraStrict\Testing\Enums\PhpType; | ||
use Nette\PhpGenerator\ClassType; | ||
use Nette\PhpGenerator\Factory; | ||
use ReflectionMethod; | ||
use ReflectionNamedType; | ||
use ReflectionUnionType; | ||
|
||
final class ExpectationMethodAssertAction | ||
{ | ||
/** | ||
* Generates a method assert in assert class. Generates __construct if $expectationClassName is passed (requires | ||
* extending AbstractExpectationCallsMap). | ||
*/ | ||
public function execute( | ||
ClassType $assertClass, | ||
ReflectionMethod $method, | ||
string $expectationClassName, | ||
PhpDocEntity $phpDoc, | ||
): void | ||
{ | ||
$parameters = $method->getParameters(); | ||
|
||
$assertMethod = (new Factory())->fromMethodReflection($method); | ||
$assertClass->addMember($assertMethod); | ||
|
||
$assertMethod->addBody(sprintf( | ||
'$_expectation = $this->getExpectation(%s);', | ||
$expectationClassName . '::class' | ||
)); | ||
|
||
$hookParameters = []; | ||
|
||
if ($parameters !== []) { | ||
$assertMethod->addBody('$_message = $this->getDebugMessage();'); | ||
$assertMethod->addBody(''); | ||
|
||
foreach ($parameters as $parameter) { | ||
$hookParameters[] = sprintf('$%s', $parameter->name); | ||
$assertMethod->addBody(sprintf( | ||
'Assert::assertEquals($_expectation->%s, $%s, $_message);', | ||
$parameter->name, | ||
$parameter->name | ||
)); | ||
} | ||
} | ||
|
||
$hookParameters[] = '$_expectation'; | ||
|
||
$assertMethod->addBody(''); | ||
|
||
$assertMethod->addBody(sprintf('if (is_callable($_expectation->%s)) {', ExpectationFileContentAction::HookProperty)); | ||
$assertMethod->addBody(sprintf( | ||
' call_user_func($_expectation->%s, %s);', | ||
ExpectationFileContentAction::HookProperty, | ||
implode(', ', $hookParameters), | ||
)); | ||
$assertMethod->addBody('}'); | ||
|
||
$returnType = $method->getReturnType(); | ||
|
||
if ($returnType instanceof ReflectionNamedType) { | ||
$enumReturnType = PhpType::tryFrom($returnType->getName()) ?? PhpType::Mixed; | ||
} elseif ($returnType instanceof ReflectionUnionType) { | ||
$enumReturnType = PhpType::Mixed; | ||
} else { | ||
$enumReturnType = $phpDoc->returnType; | ||
} | ||
|
||
switch ($enumReturnType) { | ||
case PhpType::Mixed: | ||
$assertMethod->addBody(''); | ||
$assertMethod->addBody('return $_expectation->return;'); | ||
break; | ||
case PhpType::Self: | ||
case PhpType::Static: | ||
$assertMethod->addBody(''); | ||
$assertMethod->addBody('return $this;'); | ||
break; | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$getComposerJsonDataAction -> $composerJsonDataService