-
Notifications
You must be signed in to change notification settings - Fork 12
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
Add tuple generator #71
Merged
Merged
Changes from 18 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
7ac1c2d
Add tuple generator
mtk3d ee306e3
Remove text comparing test for tuple class generator
mtk3d 2001448
Arrange Tuple methods order in the old way
mtk3d 5cbb097
Remove ReturnTypeWillChange in all files
mtk3d d7ca87a
Remove unecessery types changes
mtk3d 0470326
Add tests to fully cover Tuples
mtk3d aeedc9a
Add tuples generating validator
mtk3d 3416802
Change Tuple message
mtk3d e8c17e2
Move basic concat method to Tuple abstract class
mtk3d 67b9d79
Change size const to inline
mtk3d 286e89c
Change chema version for phpunit
mtk3d 97824b6
Fix phpstan problems
mtk3d 89c0bd8
Add generate tuples command to composer
mtk3d eb4f773
Refactor tuples generating command
mtk3d d1b694f
Add diff check to generator command
mtk3d 1dde651
Keep .tuple dir
mtk3d 33d4e4f
Keep .tuple dir
mtk3d 3947d73
Fix tuple1 formatting
mtk3d e84ba11
Merge branch 'master' into tuple-generics-proposal
akondas ebbc9d3
fixes after merge master
akondas 2a35e08
lock phpstan
akondas 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,5 @@ clover.xml | |
.idea | ||
composer.lock | ||
/.phpunit.cache/ | ||
.tuple/* | ||
!.tuple/.gitkeep | ||
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
Empty file.
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,43 @@ | ||
#!/usr/bin/env php | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
require __DIR__.'/../vendor/autoload.php'; | ||
|
||
use Munus\Generators\Tuple\TupleGeneratorConfiguration; | ||
use Munus\Tuple; | ||
|
||
const VALIDATE_OPT = 'validate'; | ||
|
||
$options = getopt('', [VALIDATE_OPT]); | ||
$validateOnly = array_key_exists(VALIDATE_OPT, $options); | ||
|
||
$tupleGenerator = TupleGeneratorConfiguration::getTupleGenerator(); | ||
$tupleGenerator->prepareTuples(Tuple::TUPLE_MAX_SIZE); | ||
|
||
$anyDiffs = false; | ||
|
||
foreach ($tupleGenerator->getPreparedTuplesNames() as $className) { | ||
passthru( | ||
sprintf('diff -u .tuple/%s.php src/Tuple/%s.php', $className, $className), | ||
$foundDiff | ||
); | ||
|
||
if ($foundDiff) { | ||
$anyDiffs = true; | ||
} | ||
} | ||
|
||
if ($anyDiffs && $validateOnly) { | ||
error_log('Differences between exising and generated Tuples found.'); | ||
error_log('Please regenerate tuples and try again.'); | ||
exit(1); | ||
} | ||
|
||
if (!$validateOnly) { | ||
$tupleGenerator->commitPreparedTuples(); | ||
print_r('Tuples successfully generated.'.PHP_EOL); | ||
} | ||
|
||
exit(0); |
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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Munus\Generators\Tuple; | ||
|
||
interface ClassPersister | ||
{ | ||
public function save(string $directory, string $className, string $content): void; | ||
|
||
public function moveClass(string $fromDir, string $toDir, string $className): void; | ||
} |
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,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Munus\Generators\Tuple; | ||
|
||
class FilePutContentsClassPersister implements ClassPersister | ||
{ | ||
public function __construct(private string $sourcePath) | ||
{ | ||
} | ||
|
||
public function save(string $directory, string $className, string $content): void | ||
{ | ||
$filePath = $this->sourcePath.$directory.'/'.$className.'.php'; | ||
file_put_contents($filePath, $content); | ||
} | ||
|
||
public function moveClass(string $fromDir, string $toDir, string $className): void | ||
{ | ||
copy(sprintf('%s/%s.php', $fromDir, $className), sprintf('%s/%s.php', $toDir, $className)); | ||
} | ||
} |
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,76 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Munus\Generators\Tuple; | ||
|
||
use Nette\PhpGenerator\ClassType; | ||
use Nette\PhpGenerator\PhpNamespace; | ||
|
||
abstract class FragmentGenerator | ||
{ | ||
private const TYPE_TEMPLATE = 'T%s'; | ||
private const VALUE_TEMPLATE = '$value%s'; | ||
private const CLASS_VALUE_TEMPLATE = '$this->value%s'; | ||
private const PARAMETER_NAMES_TEMPLATE = 'value%s'; | ||
protected const EMPTY_COMMENT_LINE = ''; | ||
|
||
abstract public function append(PhpNamespace $namespace, ClassType $class, int $tupleSize, int $maxTupleSize): void; | ||
|
||
protected function isMaxSizeTuple(int $tupleSize, int $maxTupleSize): bool | ||
{ | ||
return $tupleSize === $maxTupleSize; | ||
} | ||
|
||
protected function isTupleZero(int $tupleSize): bool | ||
{ | ||
return 0 === $tupleSize; | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
protected function types(int $tupleSize): array | ||
{ | ||
return $this->listOfTemplate(self::TYPE_TEMPLATE, $tupleSize); | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
protected function values(int $tupleSize): array | ||
{ | ||
return $this->listOfTemplate(self::VALUE_TEMPLATE, $tupleSize); | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
protected function classValues(int $tupleSize): array | ||
{ | ||
return $this->listOfTemplate(self::CLASS_VALUE_TEMPLATE, $tupleSize); | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
protected function parameterNames(int $tupleSize): array | ||
{ | ||
return $this->listOfTemplate(self::PARAMETER_NAMES_TEMPLATE, $tupleSize); | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
protected function listOfTemplate(string $template, int $tupleSize): array | ||
{ | ||
if ($this->isTupleZero($tupleSize)) { | ||
return []; | ||
} | ||
|
||
return array_map( | ||
fn (int $n): string => sprintf($template, $n), | ||
range(1, $tupleSize), | ||
); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
generators/Tuple/FragmentGenerator/AppendMethodGenerator.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Munus\Generators\Tuple\FragmentGenerator; | ||
|
||
class AppendMethodGenerator extends AppendPrependMethodAbstractGenerator | ||
{ | ||
protected function methodName(): string | ||
{ | ||
return 'append'; | ||
} | ||
|
||
protected function listOfTypes(int $tupleSize): string | ||
{ | ||
return join(', ', [...$this->types($tupleSize), 'T']); | ||
} | ||
|
||
protected function listOfValues(int $tupleSize): string | ||
{ | ||
return join(', ', [...$this->classValues($tupleSize), '$value']); | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
generators/Tuple/FragmentGenerator/AppendPrependMethodAbstractGenerator.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Munus\Generators\Tuple\FragmentGenerator; | ||
|
||
use Munus\Exception\UnsupportedOperationException; | ||
use Munus\Generators\Tuple\FragmentGenerator; | ||
use Nette\PhpGenerator\ClassType; | ||
use Nette\PhpGenerator\PhpNamespace; | ||
|
||
abstract class AppendPrependMethodAbstractGenerator extends FragmentGenerator | ||
{ | ||
public function append(PhpNamespace $namespace, ClassType $class, int $tupleSize, int $maxTupleSize): void | ||
{ | ||
$resultTupleSize = $tupleSize + 1; | ||
|
||
$method = $class->addMethod($this->methodName()); | ||
$method->addParameter('value'); | ||
|
||
if ($this->isMaxSizeTuple($tupleSize, $maxTupleSize)) { | ||
$namespace->addUse(UnsupportedOperationException::class); | ||
$method->setBody($this->getMaxTupleSizeExceptionThrowBody()); | ||
|
||
return; | ||
} | ||
|
||
$method->setReturnType($this->getMethodReturnType($namespace, $resultTupleSize)); | ||
$method->addComment('@template T'); | ||
$method->addComment(self::EMPTY_COMMENT_LINE); | ||
$method->addComment('@param T $value'); | ||
$method->addComment(self::EMPTY_COMMENT_LINE); | ||
$method->addComment($this->getReturnTypeComment($resultTupleSize, $tupleSize)); | ||
$method->setBody($this->getMethodBody($resultTupleSize, $tupleSize)); | ||
} | ||
|
||
private function getMaxTupleSizeExceptionThrowBody(): string | ||
{ | ||
return sprintf( | ||
'throw new UnsupportedOperationException(\'Can\\\'t %s next value. This is biggest possible Tuple\');', | ||
$this->methodName(), | ||
); | ||
} | ||
|
||
private function getMethodReturnType(PhpNamespace $namespace, int $resultTupleSize): string | ||
{ | ||
return sprintf('%s\Tuple%s', $namespace->getName(), $resultTupleSize); | ||
} | ||
|
||
private function getReturnTypeComment(int $resultTupleSize, int $tupleSize): string | ||
{ | ||
return sprintf('@returns Tuple%s<%s>', $resultTupleSize, $this->listOfTypes($tupleSize)); | ||
} | ||
|
||
private function getMethodBody(int $resultTupleSize, int $tupleSize): string | ||
{ | ||
return sprintf('return new Tuple%s(%s);', $resultTupleSize, $this->listOfValues($tupleSize)); | ||
} | ||
|
||
abstract protected function listOfValues(int $tupleSize): string; | ||
|
||
abstract protected function listOfTypes(int $tupleSize): string; | ||
|
||
abstract protected function methodName(): string; | ||
} |
19 changes: 19 additions & 0 deletions
19
generators/Tuple/FragmentGenerator/ArityMethodGenerator.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Munus\Generators\Tuple\FragmentGenerator; | ||
|
||
use Munus\Generators\Tuple\FragmentGenerator; | ||
use Nette\PhpGenerator\ClassType; | ||
use Nette\PhpGenerator\PhpNamespace; | ||
|
||
class ArityMethodGenerator extends FragmentGenerator | ||
{ | ||
public function append(PhpNamespace $namespace, ClassType $class, int $tupleSize, int $maxTupleSize): void | ||
{ | ||
$arity = $class->addMethod('arity'); | ||
$arity->setReturnType('int'); | ||
$arity->setBody('return ?;', [$tupleSize]); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
generators/Tuple/FragmentGenerator/ConcatMethodGenerator.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Munus\Generators\Tuple\FragmentGenerator; | ||
|
||
use Munus\Generators\Tuple\FragmentGenerator; | ||
use Nette\PhpGenerator\ClassType; | ||
use Nette\PhpGenerator\PhpNamespace; | ||
|
||
class ConcatMethodGenerator extends FragmentGenerator | ||
{ | ||
public function append(PhpNamespace $namespace, ClassType $class, int $tupleSize, int $maxTupleSize): void | ||
{ | ||
$concatableSize = $maxTupleSize - $tupleSize; | ||
|
||
foreach (range(0, $concatableSize) as $n) { | ||
$this->generateConcatTupleNMethod($n, $tupleSize, $class); | ||
} | ||
} | ||
|
||
private function generateConcatTupleNMethod(int $n, int $tupleSize, ClassType $class): void | ||
{ | ||
$returnTupleSize = $tupleSize + $n; | ||
|
||
$concatTupleN = $class->addMethod(sprintf('concatTuple%s', $n)); | ||
$concatTupleN->addParameter('tuple'); | ||
|
||
$types = $this->types($tupleSize); | ||
$uTypes = $this->listOfTemplate('U%s', $n); | ||
$bothTypes = [...$types, ...$uTypes]; | ||
|
||
$paramTupleGenerics = 0 == $n | ||
? '' | ||
: sprintf('<%s>', join(', ', $uTypes)); | ||
$returnTupleGenerics = 0 == $returnTupleSize | ||
? '' | ||
: sprintf('<%s>', join(', ', $bothTypes)); | ||
|
||
foreach ($uTypes as $uType) { | ||
$concatTupleN->addComment(sprintf('@template %s', $uType)); | ||
} | ||
|
||
if (0 !== count($uTypes)) { | ||
$concatTupleN->addComment(self::EMPTY_COMMENT_LINE); | ||
} | ||
|
||
$concatTupleN->addComment(sprintf('@param Tuple%s%s $tuple', $n, $paramTupleGenerics)); | ||
$concatTupleN->addComment(self::EMPTY_COMMENT_LINE); | ||
$concatTupleN->addComment(sprintf('@returns Tuple%s%s', $returnTupleSize, $returnTupleGenerics)); | ||
$concatTupleN->addBody('return $this->concat($tuple);'); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
generators/Tuple/FragmentGenerator/ConstructorMethodGenerator.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Munus\Generators\Tuple\FragmentGenerator; | ||
|
||
use Munus\Generators\Tuple\FragmentGenerator; | ||
use Nette\PhpGenerator\ClassType; | ||
use Nette\PhpGenerator\PhpNamespace; | ||
|
||
class ConstructorMethodGenerator extends FragmentGenerator | ||
{ | ||
public function append(PhpNamespace $namespace, ClassType $class, int $tupleSize, int $maxTupleSize): void | ||
{ | ||
$constructor = $class->addMethod('__construct'); | ||
|
||
foreach ($this->parameterNames($tupleSize) as $n => $parameterName) { | ||
$constructor->addComment(sprintf('@param T%s $%s', $n + 1, $parameterName)); | ||
$constructor->addPromotedParameter($parameterName)->setPrivate(); | ||
} | ||
} | ||
} |
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.
I'm not against it, but couldn't we use temp dir?