From aa3fe92b16d6514b5d4dd8308aa39badcd2fb46d Mon Sep 17 00:00:00 2001 From: Farrokh Ghamsary Date: Mon, 12 Dec 2022 19:58:35 +0100 Subject: [PATCH] Add abstract method as new type for class and trait --- src/MethodAbstract.php | 31 +++++++ tests/MethodAbstractTest.php | 165 +++++++++++++++++++++++++++++++++++ 2 files changed, 196 insertions(+) create mode 100644 src/MethodAbstract.php create mode 100644 tests/MethodAbstractTest.php diff --git a/src/MethodAbstract.php b/src/MethodAbstract.php new file mode 100644 index 0000000..cfe3915 --- /dev/null +++ b/src/MethodAbstract.php @@ -0,0 +1,31 @@ +signature = new Signature($name, "$modifier abstract", $returnType); + $this->dependencyAwareChildren = [$this->signature]; + } + + /** + * @return static + */ + public static function new(string $name, string $modifier = Modifier::PUBLIC, string $returnType = ''): self + { + return new static($name, $modifier, $returnType); + } + + public function generate(): string + { + return $this->buildDocBlock().$this->signature->generate(false).';'; + } + +} diff --git a/tests/MethodAbstractTest.php b/tests/MethodAbstractTest.php new file mode 100644 index 0000000..d4bc197 --- /dev/null +++ b/tests/MethodAbstractTest.php @@ -0,0 +1,165 @@ +expectOutputString('protected abstract function myMethod(): void;'); + + echo $method; + + return $method; + } + + /** + * @test + * @depends emptyBase + */ + public function addArguments(MethodAbstract $method): MethodAbstract + { + $arg1 = $method->createArgument('arg1', SplHeap::class, null)->setNullable(); + + $arg2 = $method->createArgument('arg2', 'string', ''); + $arg2->setByReference(); + + $method->add(Argument::new('arg3')); + $method->addArguments('arg4', 'arg5'); + + $this->assertEquals($arg1, $method->getArgument(1)); + $this->assertEquals($arg2, $method->getArgument(2)); + + $this->expectOutputString( + 'protected abstract function myMethod(?SplHeap $arg1 = null, string &$arg2 = \'\', $arg3, $arg4, $arg5): void;' + ); + + echo $method; + + return $method; + } + + /** + * @test + * @depends addArguments + */ + public function modifyParts(MethodAbstract $method): MethodAbstract + { + $method->setStatic(); + $method->setReturnType(Collection::class); + $method->setDocBlock(<<<'DOCBLOCK' + Another simple function + + @param SqlHeap|null $arg1 + @param string $arg2 + @param mixed $arg3 + DOCBLOCK); + + /** @var Argument $arg */ + $arg = $method->getArgument(5); + $arg->setSpread(); + + $this->assertEquals(true, $method->isStatic()); + $this->assertEquals('Collection', $method->getReturnType()); + + $this->expectOutputString(<<<'CODE' + /** + * Another simple function + * + * @param SqlHeap|null $arg1 + * @param string $arg2 + * @param mixed $arg3 + */ + protected abstract static function myMethod(?SplHeap $arg1 = null, string &$arg2 = '', $arg3, $arg4, ...$arg5): Collection; + CODE); + + echo $method; + + return $method; + } + + + /** + * @test + * @depends modifyParts + */ + public function removeParts(MethodAbstract $method): void + { + $method->removeArgument(1); + $method->removeArgument(2); + $method->removeArgument(3); + $method->unsetStatic(); + $method->clearContent(); + $method->removeDocBlock(); + + $this->expectOutputString(<<<'CODE' + protected abstract function myMethod($arg4, ...$arg5): Collection; + CODE); + + echo $method; + } + + /** + * @test + */ + public function withPromotedArguments(): MethodAbstract + { + $method = MethodAbstract::new('__construct'); + + $method->addArgument('firstName', 'string', 'Alex', Modifier::PRIVATE); + $method->addArgument('lastName', 'string', 'Kowalski', Modifier::PRIVATE); + $method->addArgument('age', 'int', Argument::NO_PARAM, Modifier::PRIVATE); + + $method->signature->setMultiline(); + + echo $method; + + $this->expectOutputString(<<<'CODE' + public abstract function __construct( + private string $firstName = 'Alex', + private string $lastName = 'Kowalski', + private int $age + ); + CODE); + + return $method; + } + + + /** + * @test + * @depends withPromotedArguments + */ + public function addNormalArguments(MethodAbstract $method): MethodAbstract + { + $method->addArgument('isStudent', 'bool'); + $method->addArgument('isEmployed'); + + echo $method; + + $this->expectOutputString(<<<'CODE' + public abstract function __construct( + private string $firstName = 'Alex', + private string $lastName = 'Kowalski', + private int $age, + bool $isStudent, + $isEmployed + ); + CODE); + + return $method; + } + +}