-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add tuple generator * Remove text comparing test for tuple class generator * Arrange Tuple methods order in the old way * Remove ReturnTypeWillChange in all files * Remove unecessery types changes * Add tests to fully cover Tuples * Add tuples generating validator * Change Tuple message * Move basic concat method to Tuple abstract class * Change size const to inline * Change chema version for phpunit * Fix phpstan problems * Add generate tuples command to composer * Refactor tuples generating command * Add diff check to generator command * Keep .tuple dir * Keep .tuple dir * Fix tuple1 formatting * fixes after merge master * lock phpstan --------- Co-authored-by: Arkadiusz Kondas <[email protected]>
- Loading branch information
Showing
36 changed files
with
2,107 additions
and
73 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 |
---|---|---|
|
@@ -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.