From 5beed9b8142ccf0cff74d685c4b886d90a8c1c63 Mon Sep 17 00:00:00 2001 From: murtukov Date: Tue, 12 Jan 2021 21:08:13 +0100 Subject: [PATCH] Common update - Change PHP requirement - Add PHPDoc blocks - Add type-hints - Other minor fixes - Make constructors final --- composer.json | 8 +++-- phpstan.neon | 8 +++++ src/AbstractFunction.php | 40 ++++++++++++++++++++- src/Argument.php | 50 ++++++++++++++++++++++---- src/ArrowFunction.php | 21 +++++++++-- src/Block.php | 9 ++++- src/Closure.php | 9 +++-- src/Collection.php | 19 +++++++--- src/Comment.php | 39 +++++++++++++++----- src/Config.php | 6 ++-- src/ConverterInterface.php | 4 +-- src/DependencyAwareGenerator.php | 13 +++++-- src/DocBlockTrait.php | 8 ++++- src/ElseBlock.php | 27 ++++++++++++++ src/ElseIfBlock.php | 33 +++++++++++++++++ src/Func.php | 2 +- src/IfElse.php | 62 +++++++------------------------- src/Instance.php | 38 ++++++++++++++++---- src/Literal.php | 7 ++-- src/Loop.php | 21 ++++++----- src/Method.php | 5 ++- src/Mock.php | 8 +++-- src/OOPStructure.php | 5 ++- src/PhpClass.php | 15 ++++++-- src/PhpInterface.php | 13 ++++++- src/PhpTrait.php | 10 +++++- src/Property.php | 29 ++++++++++++--- src/Qualifier.php | 4 +-- src/ScopedContentTrait.php | 10 ++++++ src/Signature.php | 41 ++++++++++++++++++--- src/Utils.php | 18 ++++------ tests/ArgumentTest.php | 2 +- tests/ArrowFunctionTest.php | 6 ++-- tests/ClosureTest.php | 12 ++++--- tests/CollectionTest.php | 10 +++--- tests/CommentTest.php | 6 ++-- tests/FuncTest.php | 6 ++-- tests/IfElseTest.php | 20 +++++------ tests/InstanceTest.php | 14 ++++---- tests/LoopTest.php | 2 +- tests/MethodTest.php | 16 +++++---- tests/PhpClassTest.php | 22 ++++++------ tests/PhpFileTest.php | 21 +++++++---- tests/PhpInterfaceTest.php | 9 ++--- tests/PhpTraitTest.php | 9 ++--- tests/PropertyTest.php | 2 +- tests/UtilsTest.php | 4 +-- 47 files changed, 530 insertions(+), 213 deletions(-) create mode 100644 phpstan.neon create mode 100644 src/ElseBlock.php create mode 100644 src/ElseIfBlock.php diff --git a/composer.json b/composer.json index 6979dc5..3f34ed0 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,7 @@ ], "minimum-stability": "stable", "require": { - "php": "^7.4", + "php": ">=7.4", "ext-json": "*" }, "autoload": { @@ -20,11 +20,13 @@ } }, "require-dev": { - "phpunit/phpunit": "^9.1" + "phpunit/phpunit": "^9.4.3", + "phpstan/phpstan": "^0.12.58" }, "scripts": { "test": "bin/phpunit --color=always -v --debug", - "install-cs": "test -f php-cs-fixer.phar || wget https://github.com/FriendsOfPHP/PHP-CS-Fixer/releases/download/v2.16.3/php-cs-fixer.phar -O php-cs-fixer.phar", + "install-cs": "test -f php-cs-fixer.phar || wget https://github.com/FriendsOfPHP/PHP-CS-Fixer/releases/download/v2.17.3/php-cs-fixer.phar -O php-cs-fixer.phar", + "static-analysis": "vendor/bin/phpstan analyse src tests", "fix-cs": [ "@install-cs", "@php php-cs-fixer.phar fix --diff -v --allow-risky=yes --ansi" diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..e9d4960 --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,8 @@ +parameters: + level: 8 + reportUnmatchedIgnoredErrors: false + checkMissingIterableValueType: false + checkGenericClassInNonGenericObjectType: false + paths: + - %currentWorkingDirectory%/src + - %currentWorkingDirectory%/tests diff --git a/src/AbstractFunction.php b/src/AbstractFunction.php index dc4a0dc..0d3af7c 100644 --- a/src/AbstractFunction.php +++ b/src/AbstractFunction.php @@ -13,6 +13,9 @@ public function getReturnType(): string return $this->signature->getReturnType(); } + /** + * @return $this + */ public function setReturnType(string $returnType): self { $this->signature->setReturnType($returnType); @@ -25,6 +28,9 @@ public function getArgument(int $index = 1): ?Argument return $this->signature->getArgument($index); } + /** + * @return $this + */ public function removeArgument(int $index): self { $this->signature->removeArgument($index); @@ -32,6 +38,9 @@ public function removeArgument(int $index): self return $this; } + /** + * @return $this + */ public function removeArguments(): self { $this->signature->removeArguments(); @@ -39,11 +48,19 @@ public function removeArguments(): self return $this; } + /** + * @param mixed $defaultValue + */ public function createArgument(string $name, string $type = '', $defaultValue = Argument::NO_PARAM): Argument { return $this->signature->createArgument($name, $type, $defaultValue); } + /** + * @param mixed $defaultValue + * + * @return $this + */ public function addArgument(string $name, string $type = '', $defaultValue = Argument::NO_PARAM): self { $this->signature->addArgument($name, $type, $defaultValue); @@ -51,6 +68,9 @@ public function addArgument(string $name, string $type = '', $defaultValue = Arg return $this; } + /** + * @return $this + */ public function addArguments(string ...$names): self { $this->signature->addArguments(...$names); @@ -58,6 +78,9 @@ public function addArguments(string ...$names): self return $this; } + /** + * @return $this + */ public function add(FunctionMemberInterface $member): self { $this->signature->add($member); @@ -65,6 +88,9 @@ public function add(FunctionMemberInterface $member): self return $this; } + /** + * @return $this + */ public function bindVar(string $name, bool $isByReference = false): self { $this->signature->bindVar($name, $isByReference); @@ -72,6 +98,9 @@ public function bindVar(string $name, bool $isByReference = false): self return $this; } + /** + * @return $this + */ public function bindVars(string ...$names): self { $this->signature->bindVars(...$names); @@ -79,7 +108,10 @@ public function bindVars(string ...$names): self return $this; } - public function removeBindVars() + /** + * @return $this + */ + public function removeBindVars(): self { $this->signature->removeBindVars(); @@ -91,6 +123,9 @@ public function isStatic(): bool return $this->signature->isStatic; } + /** + * @return $this + */ public function setStatic(): self { $this->signature->isStatic = true; @@ -98,6 +133,9 @@ public function setStatic(): self return $this; } + /** + * @return $this + */ public function unsetStatic() { $this->signature->isStatic = false; diff --git a/src/Argument.php b/src/Argument.php index 2ec0f72..0c2fc26 100644 --- a/src/Argument.php +++ b/src/Argument.php @@ -11,11 +11,11 @@ class Argument extends DependencyAwareGenerator implements FunctionMemberInterfa */ public const NO_PARAM = INF; - private string $type; - private string $name; - private bool $isSpread = false; - private bool $isByReference = false; - private bool $isNullable = false; + private string $type; + private string $name; + private bool $isSpread = false; + private bool $isByReference = false; + private bool $isNullable = false; /** * @var mixed @@ -26,8 +26,10 @@ class Argument extends DependencyAwareGenerator implements FunctionMemberInterfa * Argument constructor. * * @param mixed $defaultValue + * + * @throws Exception\UnrecognizedValueTypeException */ - public function __construct(string $name, string $type = '', $defaultValue = self::NO_PARAM) + public final function __construct(string $name, string $type = '', $defaultValue = self::NO_PARAM) { $this->name = $name; $this->type = $this->resolveQualifier($type); @@ -37,9 +39,15 @@ public function __construct(string $name, string $type = '', $defaultValue = sel } } + /** + * @param mixed $defaultValue + * + * @return static + * @throws Exception\UnrecognizedValueTypeException + */ public static function new(string $name, string $type = '', $defaultValue = self::NO_PARAM): self { - return new self($name, $type, $defaultValue); + return new static($name, $type, $defaultValue); } public function generate(): string @@ -80,6 +88,9 @@ public function isSpread(): bool return $this->isSpread; } + /** + * @return $this + */ public function setSpread(): self { $this->isSpread = true; @@ -87,6 +98,9 @@ public function setSpread(): self return $this; } + /** + * @return $this + */ public function unsetSpread(): self { $this->isSpread = false; @@ -99,6 +113,9 @@ public function isByReference(): bool return $this->isByReference; } + /** + * @return $this + */ public function setByReference(): self { $this->isByReference = true; @@ -106,6 +123,9 @@ public function setByReference(): self return $this; } + /** + * @return $this + */ public function unsetByReference(): self { $this->isByReference = false; @@ -113,6 +133,9 @@ public function unsetByReference(): self return $this; } + /** + * @return $this + */ public function setType(string $type): self { $this->type = $type; @@ -120,6 +143,13 @@ public function setType(string $type): self return $this; } + /** + * @param mixed $value + * + * @return $this + * + * @throws Exception\UnrecognizedValueTypeException + */ public function setDefaultValue($value): self { if (INF !== $value) { @@ -131,6 +161,9 @@ public function setDefaultValue($value): self return $this; } + /** + * @return $this + */ public function unsetNullable(): self { $this->isNullable = false; @@ -138,6 +171,9 @@ public function unsetNullable(): self return $this; } + /** + * @return $this + */ public function setNullable(): self { $this->isNullable = true; diff --git a/src/ArrowFunction.php b/src/ArrowFunction.php index 1bc9f4b..5f5e01c 100644 --- a/src/ArrowFunction.php +++ b/src/ArrowFunction.php @@ -9,7 +9,10 @@ class ArrowFunction extends AbstractFunction /** @var GeneratorInterface|string */ private $expression; - public function __construct($expression = null, string $returnType = '') + /** + * @param mixed $expression + */ + public final function __construct($expression = null, string $returnType = '') { $this->signature = new Signature('', Modifier::NONE, $returnType, 'fn'); $this->expression = $this->manageExprDependency($expression); @@ -17,9 +20,14 @@ public function __construct($expression = null, string $returnType = '') $this->dependencyAwareChildren[] = $this->signature; } - public static function new($expression = null, string $returnType = '') + /** + * @param mixed $expression + * + * @return static + */ + public static function new($expression = null, string $returnType = ''): ArrowFunction { - return new self($expression, $returnType); + return new static($expression, $returnType); } public function generate(): string @@ -39,6 +47,8 @@ public function getExpression() /** * @param mixed $expression + * + * @return $this */ public function setExpression($expression): self { @@ -47,6 +57,11 @@ public function setExpression($expression): self return $this; } + /** + * @param mixed $value + * + * @return mixed + */ protected function manageExprDependency($value) { if ($value instanceof DependencyAwareGenerator) { diff --git a/src/Block.php b/src/Block.php index 58f8093..2accc4d 100644 --- a/src/Block.php +++ b/src/Block.php @@ -8,6 +8,10 @@ class Block extends AbstractGenerator implements BlockInterface { use ScopedContentTrait; + public final function __construct() + { + } + public function generate(): string { return <<signature = new Signature('', Modifier::NONE, $returnType); $this->dependencyAwareChildren = [$this->signature]; } - public static function new(string $returnType = '') + /** + * @return static + */ + public static function new(string $returnType = ''): self { - return new self($returnType); + return new static($returnType); } public function generate(): string diff --git a/src/Collection.php b/src/Collection.php index ae98ee9..4f11d15 100644 --- a/src/Collection.php +++ b/src/Collection.php @@ -19,7 +19,7 @@ class Collection extends DependencyAwareGenerator protected Utils $utils; - public function __construct(array $items = [], bool $multiline = false, bool $withKeys = true) + private final function __construct(array $items = [], bool $multiline = false, bool $withKeys = true) { $this->items = $items; $this->multiline = $multiline; @@ -27,6 +27,9 @@ public function __construct(array $items = [], bool $multiline = false, bool $wi $this->utils = new Utils(); } + /** + * @return static + */ public static function numeric(array $items = [], bool $multiline = false): self { return new static($items, $multiline, false); @@ -35,7 +38,7 @@ public static function numeric(array $items = [], bool $multiline = false): self /** * Shorthand for `new AssocArray($items, true)`. * - * @return Collection + * @return static */ public static function assoc(array $items = [], bool $multiline = true): self { @@ -44,6 +47,8 @@ public static function assoc(array $items = [], bool $multiline = true): self /** * Creates a multiline array and adds all provided items, after applying a callback to them. + * + * @return static */ public static function map(array $items, callable $map, bool $withKeys = true): self { @@ -60,6 +65,8 @@ public static function map(array $items, callable $map, bool $withKeys = true): * Adds item to the array. * * @param mixed $value + * + * @return $this */ public function addItem(string $key, $value): self { @@ -90,6 +97,8 @@ public function addIfNotNull(string $key, $value): self * Adds item to the array if it's not empty. * * @param mixed $value + * + * @return $this */ public function addIfNotEmpty(string $key, $value): self { @@ -104,6 +113,8 @@ public function addIfNotEmpty(string $key, $value): self * Adds item to the array if it's not equal false. * * @param mixed $value + * + * @return $this */ public function addIfNotFalse(string $key, $value): self { @@ -132,7 +143,7 @@ public function ifTrue($value) return Mock::getInstance($this); } - public function getConverters() + public function getConverters(): array { return $this->converters; } @@ -165,7 +176,7 @@ public function setWithKeys(bool $val = true): self return $this; } - public function count() + public function count(): int { return count($this->items); } diff --git a/src/Comment.php b/src/Comment.php index d995e3a..43b99e9 100644 --- a/src/Comment.php +++ b/src/Comment.php @@ -14,30 +14,42 @@ class Comment extends AbstractGenerator implements BlockInterface protected string $type; protected array $lines = []; - private function __construct(string $text = '', string $type = self::TYPE_STAR) + private final function __construct(string $text = '', string $type = self::TYPE_STAR) { $this->addText($text); $this->type = $type; } + /** + * @return static + */ public static function block(string $text = ''): self { - return new self($text, self::TYPE_STAR); + return new static($text, self::TYPE_STAR); } + /** + * @return static + */ public static function hash(string $text = ''): self { - return new self($text, self::TYPE_HASH); + return new static($text, self::TYPE_HASH); } + /** + * @return static + */ public static function docBlock(string $text = ''): self { - return new self($text, self::TYPE_DOCBLOCK); + return new static($text, self::TYPE_DOCBLOCK); } + /** + * @return static + */ public static function slash(string $text = ''): self { - return new self($text, self::TYPE_SLASH); + return new static($text, self::TYPE_SLASH); } public function generate(): string @@ -45,7 +57,7 @@ public function generate(): string return $this->{"build$this->type"}(); } - private function buildStar() + private function buildStar(): string { $lines = implode("\n * ", $this->lines); @@ -56,7 +68,7 @@ private function buildStar() CODE; } - private function buildDocBlock() + private function buildDocBlock(): string { $lines = implode("\n * ", $this->lines); @@ -67,16 +79,19 @@ private function buildDocBlock() CODE; } - private function buildHash() + private function buildHash(): string { return '# '.implode("\n# ", $this->lines); } - private function buildSlash() + private function buildSlash(): string { return '// '.implode("\n// ", $this->lines); } + /** + * @return $this + */ public function addLine(string $text): self { $this->lines[] = $text; @@ -84,6 +99,9 @@ public function addLine(string $text): self return $this; } + /** + * @return $this + */ public function addEmptyLine(): self { $this->lines[] = ''; @@ -91,6 +109,9 @@ public function addEmptyLine(): self return $this; } + /** + * @return $this + */ public function addText(string $text): self { if ('' === $text) { diff --git a/src/Config.php b/src/Config.php index 9c66abf..6105515 100644 --- a/src/Config.php +++ b/src/Config.php @@ -31,7 +31,7 @@ class Config /** * Registers user defined stringifiers. */ - public static function registerConverter(ConverterInterface $converter, string $type) + public static function registerConverter(ConverterInterface $converter, string $type): void { $fqcn = get_class($converter); @@ -44,7 +44,7 @@ public static function registerConverter(ConverterInterface $converter, string $ * * @param string $fqcn - Fully qualified class name */ - public static function unregisterConverter(string $fqcn) + public static function unregisterConverter(string $fqcn): void { // Remove instance unset(self::$customStringifiers[$fqcn]); @@ -63,7 +63,7 @@ public static function getConverter(string $fqcn): ?object return self::$customStringifiers[$fqcn] ?? null; } - public static function getConverterClasses(string $type): ?array + public static function getConverterClasses(string $type): array { return self::$customStringifiersTypeMap[$type] ?? []; } diff --git a/src/ConverterInterface.php b/src/ConverterInterface.php index 2ff9911..cfdf25b 100644 --- a/src/ConverterInterface.php +++ b/src/ConverterInterface.php @@ -14,7 +14,7 @@ interface ConverterInterface const TYPE_ARRAY = 'array'; /** - * @param $value + * @param mixed $value * * @return mixed */ @@ -23,7 +23,7 @@ public function convert($value); /** * Checks, whether the value should be converted. * - * @param $value + * @param mixed $value */ public function check($value): bool; } diff --git a/src/DependencyAwareGenerator.php b/src/DependencyAwareGenerator.php index e387334..86312a5 100644 --- a/src/DependencyAwareGenerator.php +++ b/src/DependencyAwareGenerator.php @@ -17,7 +17,7 @@ abstract class DependencyAwareGenerator extends AbstractGenerator */ protected array $dependencyAwareChildren = []; - public function resolveQualifier(string $path, $alias = ''): string + public function resolveQualifier(string $path, string $alias = ''): string { if (empty($path) || false === Config::$shortenQualifiers || '\\' === $path[0]) { return $path; @@ -35,6 +35,9 @@ public function resolveQualifier(string $path, $alias = ''): string return $path; } + /** + * @return $this + */ public function addUse(string $fqcn, string ...$aliases): self { $this->usePaths[$fqcn] = implode(', ', $aliases); @@ -42,6 +45,9 @@ public function addUse(string $fqcn, string ...$aliases): self return $this; } + /** + * @return $this + */ public function addUseGroup(string $fqcn, string ...$classNames) { foreach ($classNames as $name) { @@ -57,6 +63,9 @@ public function addUseGroup(string $fqcn, string ...$classNames) return $this; } + /** + * @return $this + */ public function removeUse(string $fqcn): self { unset($this->usePaths[$fqcn]); @@ -65,7 +74,7 @@ public function removeUse(string $fqcn): self return $this; } - public function useGroupsToArray() + public function useGroupsToArray(): array { $result = []; diff --git a/src/DocBlockTrait.php b/src/DocBlockTrait.php index 0299b23..c80d7a8 100644 --- a/src/DocBlockTrait.php +++ b/src/DocBlockTrait.php @@ -13,6 +13,9 @@ public function getDocBlock(): ?Comment return $this->docBlock; } + /** + * @return $this + */ public function setDocBlock(string $text): self { $this->docBlock = Comment::docBlock($text); @@ -25,6 +28,9 @@ public function createDocBlock(string $text = ''): Comment return $this->docBlock = Comment::docBlock($text); } + /** + * @return $this + */ public function removeDocBlock(): self { $this->docBlock = null; @@ -32,7 +38,7 @@ public function removeDocBlock(): self return $this; } - protected function buildDocBlock() + protected function buildDocBlock(): string { $code = ''; diff --git a/src/ElseBlock.php b/src/ElseBlock.php new file mode 100644 index 0000000..3b16027 --- /dev/null +++ b/src/ElseBlock.php @@ -0,0 +1,27 @@ +parent = $parent; + } + + public function end(): IfElse + { + return $this->parent; + } + + public function generate(): string + { + return " else {\n{$this->generateContent()}\n}"; + } +} \ No newline at end of file diff --git a/src/ElseIfBlock.php b/src/ElseIfBlock.php new file mode 100644 index 0000000..b3d1d52 --- /dev/null +++ b/src/ElseIfBlock.php @@ -0,0 +1,33 @@ +expression = $expression; + $this->parent = $parent; + } + + public function generate(): string + { + return " elseif ($this->expression) {\n{$this->generateContent()}\n}"; + } + + public function end(): IfElse + { + return $this->parent; + } +} \ No newline at end of file diff --git a/src/Func.php b/src/Func.php index 6ae59bf..d008271 100644 --- a/src/Func.php +++ b/src/Func.php @@ -9,7 +9,7 @@ class Func extends AbstractFunction implements BlockInterface use ScopedContentTrait; use DocBlockTrait; - public function __construct(string $name, string $returnType = '') + public final function __construct(string $name, string $returnType = '') { $this->signature = new Signature($name, Modifier::NONE, $returnType); $this->dependencyAwareChildren = [$this->signature]; diff --git a/src/IfElse.php b/src/IfElse.php index d628bde..961fb99 100644 --- a/src/IfElse.php +++ b/src/IfElse.php @@ -11,22 +11,26 @@ class IfElse extends AbstractGenerator implements BlockInterface /** @var GeneratorInterface|string */ private $expression; - /** @var GeneratorInterface[] */ + /** @var ElseIfBlock[] */ private array $elseIfBlocks = []; - private ?GeneratorInterface $elseBlock = null; + private ?ElseBlock $elseBlock = null; /** * @param GeneratorInterface|string $ifExpression */ - public function __construct($ifExpression = '') + public final function __construct($ifExpression = '') { $this->expression = $ifExpression; } + /** + * @param string $ifExpression + * @return static + */ public static function new($ifExpression = ''): self { - return new self($ifExpression); + return new static($ifExpression); } /** @@ -54,55 +58,13 @@ public function generate(): string /** * @param GeneratorInterface|string $expression */ - public function createElseIf($expression = ''): object + public function createElseIf($expression = ''): ElseIfBlock { - return $this->elseIfBlocks[] = new class($expression, $this) extends DependencyAwareGenerator { - use ScopedContentTrait; - - /** @var GeneratorInterface|string */ - public $expression; - - public IfElse $parent; - - public function __construct($expression, $parent) - { - $this->expression = $expression; - $this->parent = $parent; - } - - public function generate(): string - { - return " elseif ($this->expression) {\n{$this->generateContent()}\n}"; - } - - public function end() - { - return $this->parent; - } - }; + return $this->elseIfBlocks[] = new ElseIfBlock($expression, $this); } - public function createElse(): object + public function createElse(): ElseBlock { - return $this->elseBlock = new class($this) extends DependencyAwareGenerator { - use ScopedContentTrait; - - public IfElse $parent; - - public function __construct($parent) - { - $this->parent = $parent; - } - - public function end() - { - return $this->parent; - } - - public function generate(): string - { - return " else {\n{$this->generateContent()}\n}"; - } - }; + return $this->elseBlock = new ElseBlock($this); } } diff --git a/src/Instance.php b/src/Instance.php index 14e9df9..2dd1f72 100644 --- a/src/Instance.php +++ b/src/Instance.php @@ -8,9 +8,12 @@ class Instance extends DependencyAwareGenerator { private array $args; private string $qualifier; - public bool $multiline = false; + public bool $multiline = false; - public function __construct(string $qualifier, ...$args) + /** + * @param mixed ...$args + */ + public final function __construct(string $qualifier, ...$args) { $this->qualifier = $this->resolveQualifier($qualifier); $this->args = $args; @@ -46,6 +49,11 @@ public function generate(): string return "new $this->qualifier($args)"; } + /** + * @param mixed $arg + * + * @return $this + */ public function addArgument($arg): self { $this->args[] = $arg; @@ -53,19 +61,32 @@ public function addArgument($arg): self return $this; } - public static function multiline(string $qualifier, ...$args) + /** + * @param mixed ...$args + * + * @return static + */ + public static function multiline(string $qualifier, ...$args): self { - $instance = new self($qualifier, ...$args); + $instance = new static($qualifier, ...$args); $instance->multiline = true; return $instance; } - public static function new(string $qualifier, ...$args) + /** + * @param mixed ...$args + * + * @return static + */ + public static function new(string $qualifier, ...$args): self { - return new self($qualifier, ...$args); + return new static($qualifier, ...$args); } + /** + * @return $this + */ public function setMultiline(): self { $this->multiline = true; @@ -73,7 +94,10 @@ public function setMultiline(): self return $this; } - public function setInline() + /** + * @return $this + */ + public function setInline(): self { $this->multiline = false; diff --git a/src/Literal.php b/src/Literal.php index 590eeff..d387ba2 100644 --- a/src/Literal.php +++ b/src/Literal.php @@ -8,12 +8,15 @@ class Literal extends AbstractGenerator { private string $value; - public function __construct(string $value) + public final function __construct(string $value) { $this->value = $value; } - public static function new(string $value) + /** + * @return static + */ + public static function new(string $value): self { return new static($value); } diff --git a/src/Loop.php b/src/Loop.php index 5ce5624..dfa1ece 100644 --- a/src/Loop.php +++ b/src/Loop.php @@ -16,7 +16,7 @@ class Loop extends DependencyAwareGenerator implements BlockInterface private string $condition; private string $type; - public function __construct(string $condition = '', $type = self::TYPE_WHILE) + public final function __construct(string $condition = '', string $type = self::TYPE_WHILE) { $this->condition = $condition; $this->type = $type; @@ -41,23 +41,26 @@ public function generate(): string CODE; } - public static function while(string $condition) + public static function while(string $condition): self { - return new self($condition); + return new static($condition); } - public static function for(string $condition) + public static function for(string $condition): self { - return new self($condition, self::TYPE_FOR); + return new static($condition, self::TYPE_FOR); } - public static function foreach(string $condition) + public static function foreach(string $condition): self { - return new self($condition, self::TYPE_FOREACH); + return new static($condition, self::TYPE_FOREACH); } - public static function doWhile(string $condition) + /** + * @return static + */ + public static function doWhile(string $condition): self { - return new self($condition, self::TYPE_DO_WHILE); + return new static($condition, self::TYPE_DO_WHILE); } } diff --git a/src/Method.php b/src/Method.php index 2847bf0..461fdaa 100644 --- a/src/Method.php +++ b/src/Method.php @@ -9,12 +9,15 @@ class Method extends AbstractFunction implements BlockInterface use ScopedContentTrait; use DocBlockTrait; - public function __construct(string $name, string $modifier = Modifier::PUBLIC, string $returnType = '') + public final function __construct(string $name, string $modifier = Modifier::PUBLIC, string $returnType = '') { $this->signature = new Signature($name, $modifier, $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); diff --git a/src/Mock.php b/src/Mock.php index c5fffe5..f01c0f2 100644 --- a/src/Mock.php +++ b/src/Mock.php @@ -4,20 +4,22 @@ /** * Helper class for conditional builders. + * + * @method addItem(string $key, mixed $value) */ class Mock { - private static object $caller; + private static Collection $caller; private static self $instance; - public static function getInstance(object $caller): self + public static function getInstance(Collection $caller): self { self::$caller = $caller; return self::$instance ?? self::$instance = new self(); } - public function __call($name, $arguments) + public function __call(string $name, array $arguments): Collection { return self::$caller; } diff --git a/src/OOPStructure.php b/src/OOPStructure.php index 988bb68..d44aafb 100644 --- a/src/OOPStructure.php +++ b/src/OOPStructure.php @@ -11,11 +11,14 @@ abstract class OOPStructure extends DependencyAwareGenerator public string $name; - public function __construct(string $name) + public final function __construct(string $name) { $this->name = $name; } + /** + * @return static + */ public static function new(string $name): self { return new static($name); diff --git a/src/PhpClass.php b/src/PhpClass.php index 8b9f60c..4c223df 100644 --- a/src/PhpClass.php +++ b/src/PhpClass.php @@ -59,13 +59,18 @@ public function setName(string $name): self /** * @param mixed $value + * + * @return $this */ public function addConst(string $name, $value, string $modifier = Modifier::PUBLIC): self { - return $this - ->append(Property::new($name, $modifier, '', $value)->setConst()); + return $this->append(Property::new($name, $modifier, '', $value)->setConst()); } + /** + * @param mixed $defaulValue + * @return $this + */ public function addProperty(string $name, string $modifier = Modifier::PUBLIC, string $type = '', $defaulValue = ''): self { return $this->append(new Property($name, $modifier, $type, $defaulValue)); @@ -135,6 +140,9 @@ public function setFinal(): self return $this; } + /** + * @return $this + */ public function unsetFinal(): self { $this->isFinal = false; @@ -147,6 +155,9 @@ public function isAbstract(): bool return $this->isAbstract; } + /** + * @return $this + */ public function setAbstract(): self { $this->isAbstract = true; diff --git a/src/PhpInterface.php b/src/PhpInterface.php index 282807b..4133417 100644 --- a/src/PhpInterface.php +++ b/src/PhpInterface.php @@ -35,24 +35,35 @@ public function createSignature(string $name, string $returnType = ''): Signatur return $signature; } + /** + * @return $this + */ public function addSignature(string $name, string $returnType = ''): self { return $this->append(new Signature($name, Modifier::PUBLIC, $returnType)); } - public function addSignatureFromMethod(Method $method) + /** + * @return $this + */ + public function addSignatureFromMethod(Method $method): self { return $this->append($method->signature); } /** * @param mixed $value + * + * @return $this */ public function addConst(string $name, $value): self { return $this->append(Property::new($name, Modifier::PUBLIC, '', $value)->setConst()); } + /** + * @return $this + */ public function addExtends(string ...$extends) { foreach ($extends as $extend) { diff --git a/src/PhpTrait.php b/src/PhpTrait.php index ab9125a..aa55935 100644 --- a/src/PhpTrait.php +++ b/src/PhpTrait.php @@ -6,12 +6,20 @@ class PhpTrait extends OOPStructure { + /** + * @param mixed $defaulValue + * + * @return $this + */ public function addProperty(string $name, string $modifier = Modifier::PUBLIC, string $type = '', $defaulValue = Property::NO_PARAM): self { return $this->append(new Property($name, $modifier, $type, $defaulValue)); } - public function createProperty(string $name, string $modifier = Modifier::PUBLIC, string $type = '', $defaulValue = Property::NO_PARAM) + /** + * @param mixed $defaulValue + */ + public function createProperty(string $name, string $modifier = Modifier::PUBLIC, string $type = '', $defaulValue = Property::NO_PARAM): Property { $property = new Property($name, $modifier, $type, $defaulValue); diff --git a/src/Property.php b/src/Property.php index 1076b6c..db57a40 100644 --- a/src/Property.php +++ b/src/Property.php @@ -21,7 +21,12 @@ class Property extends DependencyAwareGenerator private string $modifier; private string $typeHint; - public function __construct(string $name, ?string $modifier, string $typeHint = '', $defaultValue = self::NO_PARAM) + /** + * @param mixed $defaultValue + * + * @throws Exception\UnrecognizedValueTypeException + */ + public final function __construct(string $name, ?string $modifier, string $typeHint = '', $defaultValue = self::NO_PARAM) { $this->name = $name; $this->modifier = $modifier ?? Modifier::PUBLIC; @@ -36,12 +41,19 @@ public function __construct(string $name, ?string $modifier, string $typeHint = } } + /** + * @param mixed $value + * + * @return static + * + * @throws Exception\UnrecognizedValueTypeException + */ public static function new( string $name, ?string $modifier = Modifier::PUBLIC, string $typeHint = '', $value = self::NO_PARAM - ) { + ): self { return new static($name, $modifier, $typeHint, $value); } @@ -89,6 +101,13 @@ public function getDefaultValue(): string return $this->value; } + /** + * @param mixed $value + * + * @return $this + * + * @throws Exception\UnrecognizedValueTypeException + */ public function setDefaultValue($value): self { $this->value = Utils::stringify($value); @@ -119,7 +138,7 @@ public function setConst(): self return $this; } - public function unsetConst() + public function unsetConst(): self { $this->isConst = false; @@ -159,12 +178,12 @@ public function setTypeHint(string $typeHint): self return $this; } - public function setNullable() + public function setNullable(): void { $this->isNullable = true; } - public function unsetNullable() + public function unsetNullable(): void { $this->isNullable = false; } diff --git a/src/Qualifier.php b/src/Qualifier.php index 3d48ef2..596f2fa 100644 --- a/src/Qualifier.php +++ b/src/Qualifier.php @@ -8,12 +8,12 @@ class Qualifier extends DependencyAwareGenerator { private string $name; - public function __construct(string $className) + public final function __construct(string $className) { $this->name = $this->resolveQualifier($className); } - public static function new(string $className) + public static function new(string $className): self { return new static($className); } diff --git a/src/ScopedContentTrait.php b/src/ScopedContentTrait.php index 7a96011..0ed4aed 100644 --- a/src/ScopedContentTrait.php +++ b/src/ScopedContentTrait.php @@ -16,6 +16,8 @@ trait ScopedContentTrait /** * @param GeneratorInterface|string ...$values + * + * @return $this */ public function append(...$values): self { @@ -36,6 +38,8 @@ public function append(...$values): self /** * @param GeneratorInterface|string ...$values + * + * @return $this */ public function prepend(...$values): self { @@ -54,6 +58,9 @@ public function prepend(...$values): self return $this; } + /** + * @return $this + */ public function emptyLine(): self { $this->content[] = []; @@ -61,6 +68,9 @@ public function emptyLine(): self return $this; } + /** + * @return $this + */ public function clearContent(): self { $this->content = []; diff --git a/src/Signature.php b/src/Signature.php index 2a3f0a9..ae8d8a5 100644 --- a/src/Signature.php +++ b/src/Signature.php @@ -38,6 +38,9 @@ public function getReturnType(): string return $this->returnType; } + /** + * @return $this + */ public function setReturnType(string $returnType): self { $this->returnType = $this->resolveQualifier($returnType); @@ -50,6 +53,8 @@ public function setReturnType(string $returnType): self * If they are requested, they are first converted into objects then * returned back. * + * @throws Exception\UnrecognizedValueTypeException + * * @return Argument */ public function getArgument(int $index = 1): ?Argument @@ -78,18 +83,33 @@ public function removeArgument(int $index): self return $this; } - public function removeArguments() + /** + * @return $this + */ + public function removeArguments(): self { $this->args = []; return $this; } + /** + * @param mixed $defaultValue + * + * @throws Exception\UnrecognizedValueTypeException + */ public function createArgument(string $name, string $type = '', $defaultValue = Argument::NO_PARAM): Argument { return $this->args[] = new Argument($name, $type, $defaultValue); } + /** + * @param mixed$defaultValue + * + * @throws Exception\UnrecognizedValueTypeException + * + * @return $this + */ public function addArgument(string $name, string $type = '', $defaultValue = Argument::NO_PARAM): self { if (1 === func_num_args()) { @@ -101,7 +121,11 @@ public function addArgument(string $name, string $type = '', $defaultValue = Arg return $this; } - public function addArguments(string ...$names) + /** + * @return $this + * @throws Exception\UnrecognizedValueTypeException + */ + public function addArguments(string ...$names): self { foreach ($names as $name) { $this->addArgument($name); @@ -110,6 +134,9 @@ public function addArguments(string ...$names) return $this; } + /** + * @return $this + */ public function add(FunctionMemberInterface $member): self { if ($member instanceof Argument) { @@ -119,6 +146,9 @@ public function add(FunctionMemberInterface $member): self return $this; } + /** + * @return $this + */ public function bindVar(string $name, bool $isByReference = false): self { $name = ltrim($name, '$'); @@ -128,7 +158,10 @@ public function bindVar(string $name, bool $isByReference = false): self return $this; } - public function bindVars(string ...$names) + /** + * @return $this + */ + public function bindVars(string ...$names): self { foreach ($names as $name) { $this->bindVar($name); @@ -161,7 +194,7 @@ public function generate(bool $withDocBlock = true): string return "{$docBlock}{$modifier}{$isStatic}{$this->qualifier}{$this->name}($args){$uses}{$returnType}"; } - public function removeBindVars() + public function removeBindVars(): void { $this->uses = []; } diff --git a/src/Utils.php b/src/Utils.php index 4b874ed..0934e63 100644 --- a/src/Utils.php +++ b/src/Utils.php @@ -84,13 +84,9 @@ private static function stringifyValue($value, bool $topLevel = false): string case 'boolean': case 'integer': case 'double': - return json_encode($value); + return (string) json_encode($value); case 'string': - if ('' === $value) { - return "''"; - } - - return self::filterString($value); + return ('' === $value) ? "''" : self::filterString($value); case 'array': if (empty($value)) { return '[]'; @@ -110,7 +106,6 @@ private static function stringifyValue($value, bool $topLevel = false): string if (!$value instanceof GeneratorInterface) { try { $result = json_encode($value->__toString()); - return false !== $result ? $result : '[object]'; } catch (Error $e) { $class = get_class($value); @@ -120,11 +115,7 @@ private static function stringifyValue($value, bool $topLevel = false): string return (string) $value; case 'NULL': - if (self::$skipNullValues) { - return ''; - } - - return 'null'; + return self::$skipNullValues ? '' : 'null'; default: throw new UnrecognizedValueTypeException('Cannot stringify value of unrecognized type.'); } @@ -214,6 +205,9 @@ public static function indent(string $code, bool $leadingIndent = true): string return $code; } + /** + * @return false|string + */ public static function resolveQualifier(string $path) { if ($portion = strrchr($path, '\\')) { diff --git a/tests/ArgumentTest.php b/tests/ArgumentTest.php index 828e1f2..bac2fae 100644 --- a/tests/ArgumentTest.php +++ b/tests/ArgumentTest.php @@ -10,7 +10,7 @@ class ArgumentTest extends TestCase /** * @test */ - public function emptyBase() + public function emptyBase(): Argument { $argument = Argument::new('arg1', SplHeap::class, null) ->setNullable() diff --git a/tests/ArrowFunctionTest.php b/tests/ArrowFunctionTest.php index a2c915d..d1ff7cc 100644 --- a/tests/ArrowFunctionTest.php +++ b/tests/ArrowFunctionTest.php @@ -10,7 +10,7 @@ class ArrowFunctionTest extends TestCase /** * @test */ - public function emptyBody() + public function emptyBody(): ArrowFunction { $arrow = ArrowFunction::new(); @@ -27,7 +27,7 @@ public function emptyBody() * @test * @depends emptyBody */ - public function setExpression(ArrowFunction $arrow) + public function setExpression(ArrowFunction $arrow): array { $innerArrow = ArrowFunction::new([ 'name' => 'Alrik', @@ -56,7 +56,7 @@ public function setExpression(ArrowFunction $arrow) * @test * @depends setExpression */ - public function setStatic(array $values) + public function setStatic(array $values): void { [$arrow, $template] = $values; diff --git a/tests/ClosureTest.php b/tests/ClosureTest.php index 7296914..988fb25 100644 --- a/tests/ClosureTest.php +++ b/tests/ClosureTest.php @@ -4,6 +4,7 @@ use Murtukov\PHPCodeGenerator\Argument; use Murtukov\PHPCodeGenerator\Closure; +use Murtukov\PHPCodeGenerator\Exception\UnrecognizedValueTypeException; use Murtukov\PHPCodeGenerator\Loop; use PHPUnit\Framework\TestCase; @@ -12,7 +13,7 @@ class ClosureTest extends TestCase /** * @test */ - public function emptyBase() + public function emptyBase(): Closure { $closure = Closure::new('array'); @@ -30,8 +31,9 @@ function (): array { /** * @test * @depends emptyBase + * @throws UnrecognizedValueTypeException */ - public function addArguments(CLosure $closure) + public function addArguments(CLosure $closure): Closure { $closure->addArgument('value'); @@ -58,7 +60,7 @@ function ($value, array $options = [], bool $filter = false): array { * @test * @depends addArguments */ - public function bindVars(Closure $closure) + public function bindVars(Closure $closure): Closure { $closure->bindVars('this', 'name'); $closure->bindVar('global', true); @@ -78,7 +80,7 @@ function ($value, array $options = [], bool $filter = false) use ($this, $name, * @test * @depends bindVars */ - public function addContent(Closure $closure) + public function addContent(Closure $closure): Closure { $foreach = Loop::foreach('$options as &$option') ->append('unset($option)'); @@ -102,7 +104,7 @@ function ($value, array $options = [], bool $filter = false) use ($this, $name, * @test * @depends addContent */ - public function modifyParts(Closure $closure) + public function modifyParts(Closure $closure): void { $closure->setStatic(); $closure->removeArguments(); diff --git a/tests/CollectionTest.php b/tests/CollectionTest.php index 4b2483f..e294482 100644 --- a/tests/CollectionTest.php +++ b/tests/CollectionTest.php @@ -28,7 +28,7 @@ public function emptyAssoc(): Collection * @test * @depends emptyAssoc */ - public function addItemsAssoc(Collection $collection) + public function addItemsAssoc(Collection $collection): void { $collection ->push('pushedValue') @@ -87,7 +87,7 @@ public function addItemsAssoc(Collection $collection) /** * @test */ - public function conditionalAdd() + public function conditionalAdd(): void { $collection = Collection::assoc(); @@ -135,7 +135,7 @@ public function conditionalAdd() /** * @test */ - public function mapCollection() + public function mapCollection(): void { $array = [ 'constraints' => ['NotNull', 'Length', 'Range'], @@ -201,7 +201,7 @@ public function mapCollection() /** * @test */ - public function stringifyWithCustomConverter() + public function stringifyWithCustomConverter(): void { $converter = new class() implements ConverterInterface { public function convert($value) @@ -252,7 +252,7 @@ public function check($string): bool /** * @test */ - public function orderBy() + public function orderBy(): void { $collection = Collection::assoc(); diff --git a/tests/CommentTest.php b/tests/CommentTest.php index 784543d..99d2118 100644 --- a/tests/CommentTest.php +++ b/tests/CommentTest.php @@ -11,7 +11,7 @@ class CommentTest extends TestCase /** * @test */ - public function starCommentMultiline() + public function starCommentMultiline(): void { $expected = <<assertEquals( "# $this->firstLine", @@ -39,7 +39,7 @@ public function hashComment() /** * @test */ - public function hashCommentMultiline() + public function hashCommentMultiline(): void { $comment = Comment::hash($this->firstLine); $comment->addEmptyLine(); diff --git a/tests/FuncTest.php b/tests/FuncTest.php index 30415e5..aa28660 100644 --- a/tests/FuncTest.php +++ b/tests/FuncTest.php @@ -12,7 +12,7 @@ class FuncTest extends TestCase /** * @test */ - public function emptyBase() + public function emptyBase(): Func { $func = Func::new('myMethod', 'void'); @@ -32,7 +32,7 @@ function myMethod(): void * @test * @depends emptyBase */ - public function addContent(Func $func) + public function addContent(Func $func): Func { $func->append('foreach ($users as $user) ', Block::new()); @@ -54,7 +54,7 @@ function myMethod(): void * @test * @depends addContent */ - public function addArguments(Func $func) + public function addArguments(Func $func): Func { $func->createArgument('arg1', SplHeap::class, null)->setNullable(); $func->createArgument('arg2', 'string', ''); diff --git a/tests/IfElseTest.php b/tests/IfElseTest.php index 2dc4cca..55305aa 100644 --- a/tests/IfElseTest.php +++ b/tests/IfElseTest.php @@ -9,7 +9,7 @@ class IfElseTest extends TestCase { /** @test */ - public function withoutElseEmptyContent() + public function withoutElseEmptyContent(): void { $ifElse = IfElse::new('"name" === 15'); @@ -23,7 +23,7 @@ public function withoutElseEmptyContent() } /** @test */ - public function withElseEmptyContent() + public function withElseEmptyContent(): void { $ifElse = IfElse::new('true'); $ifElse->createElse(); @@ -40,7 +40,7 @@ public function withElseEmptyContent() } /** @test */ - public function withElseElseIfEmptyContent() + public function withElseElseIfEmptyContent(): void { $ifElse = IfElse::new('true'); $ifElse->createElse(); @@ -60,7 +60,7 @@ public function withElseElseIfEmptyContent() } /** @test */ - public function allPartsWithContent() + public function allPartsWithContent(): void { $ifElse = IfElse::new(); $ifElse->setExpression('$name === 15'); @@ -76,15 +76,15 @@ public function allPartsWithContent() ->append('return false') ->end(); - $expected = << 'Timur']; - } elseif ('\$name === 95') { + $expected = <<<'CODE' + if ($name === 15) { + $names = ['name' => 'Timur']; + } elseif ('$name === 95') { return null; - } elseif (\$name === 95) { + } elseif ($name === 95) { return "false"; } else { - \$x = 95; + $x = 95; return false; } CODE; diff --git a/tests/InstanceTest.php b/tests/InstanceTest.php index 32c0127..a459f8e 100644 --- a/tests/InstanceTest.php +++ b/tests/InstanceTest.php @@ -11,7 +11,7 @@ class InstanceTest extends TestCase /** * @test */ - public function withoutArgs() + public function withoutArgs(): void { $instance = Instance::new(DateTime::class); @@ -25,7 +25,7 @@ public function withoutArgs() /** * @test */ - public function withOneArg() + public function withOneArg(): Instance { $instance = Instance::new('User', 'Andrew'); @@ -42,7 +42,7 @@ public function withOneArg() * @test * @depends withOneArg */ - public function addSecondArgument(Instance $instance) + public function addSecondArgument(Instance $instance): void { $instance->addArgument('Jameson'); @@ -56,7 +56,7 @@ public function addSecondArgument(Instance $instance) /** * @test */ - public function shortenNamespace() + public function shortenNamespace(): void { $instance = Instance::new('App\Entity\User'); @@ -70,7 +70,7 @@ public function shortenNamespace() /** * @test */ - public function suppressShorteningWithCustomSymbol() + public function suppressShorteningWithCustomSymbol(): void { Config::$suppressSymbol = '~'; $instance = Instance::new('~App\Entity\User'); @@ -88,7 +88,7 @@ public function suppressShorteningWithCustomSymbol() /** * @test */ - public function suppressShortening() + public function suppressShortening(): void { $instance = Instance::new('@App\Entity\User'); @@ -102,7 +102,7 @@ public function suppressShortening() /** * @test */ - public function differentArgTypes() + public function differentArgTypes(): Instance { $instance = Instance::multiline('Test', null, [], new Instance('DateTime'), false); diff --git a/tests/LoopTest.php b/tests/LoopTest.php index 359f6ce..afdc7fa 100644 --- a/tests/LoopTest.php +++ b/tests/LoopTest.php @@ -11,7 +11,7 @@ class LoopTest extends TestCase /** * @test */ - public function allLoops() + public function allLoops(): void { $for = Loop::for('$i = 1; $i < 1000; ++$i') ->append('$x = $i') diff --git a/tests/MethodTest.php b/tests/MethodTest.php index f9d28c0..5de1cd8 100644 --- a/tests/MethodTest.php +++ b/tests/MethodTest.php @@ -14,7 +14,7 @@ class MethodTest extends TestCase /** * @test */ - public function emptyBase() + public function emptyBase(): Method { $method = Method::new('myMethod', Modifier::PRIVATE, 'void'); @@ -34,7 +34,7 @@ private function myMethod(): void * @test * @depends emptyBase */ - public function addContent(Method $method) + public function addContent(Method $method): Method { $method->append('$object = ', Instance::new(stdClass::class)); @@ -54,12 +54,12 @@ private function myMethod(): void * @test * @depends addContent */ - public function addArguments(Method $method) + public function addArguments(Method $method): Method { $arg1 = $method->createArgument('arg1', SplHeap::class, null)->setNullable(); $arg2 = $method->createArgument('arg2', 'string', ''); - $arg2->setByReference(true); + $arg2->setByReference(); $method->add(Argument::new('arg3')); $method->addArguments('arg4', 'arg5'); @@ -83,7 +83,7 @@ private function myMethod(?SplHeap $arg1 = null, string &$arg2 = '', $arg3, $arg * @test * @depends addArguments */ - public function modifyParts(Method $method) + public function modifyParts(Method $method): Method { $method->setStatic(); $method->setReturnType(Collection::class); @@ -95,7 +95,9 @@ public function modifyParts(Method $method) @param mixed $arg3 DOCBLOCK); - $method->getArgument(5)->setSpread(true); + /** @var Argument $arg */ + $arg = $method->getArgument(5); + $arg->setSpread(); $this->assertEquals(true, $method->isStatic()); $this->assertEquals('Collection', $method->getReturnType()); @@ -123,7 +125,7 @@ private static function myMethod(?SplHeap $arg1 = null, string &$arg2 = '', $arg * @test * @depends modifyParts */ - public function removeParts(Method $method) + public function removeParts(Method $method): void { $method->removeArgument(1); $method->removeArgument(2); diff --git a/tests/PhpClassTest.php b/tests/PhpClassTest.php index 8cf8762..a9f403b 100644 --- a/tests/PhpClassTest.php +++ b/tests/PhpClassTest.php @@ -2,6 +2,7 @@ declare(strict_types=1); +use Murtukov\PHPCodeGenerator\Comment; use Murtukov\PHPCodeGenerator\Literal; use Murtukov\PHPCodeGenerator\Method; use Murtukov\PHPCodeGenerator\Modifier; @@ -13,7 +14,7 @@ class PhpClassTest extends TestCase /** * @test */ - public function emptyBase() + public function emptyBase(): PhpClass { $code = <<addImplements(JsonSerializable::class, ArrayAccessible::class); + $class->addImplements(JsonSerializable::class, ArrayAccess::class); $this->assertEquals($code, $class->generate()); return $class; @@ -70,10 +71,10 @@ class Stringifier extends SplStack implements JsonSerializable, ArrayAccessible * @test * @depends addImplements */ - public function addProperties(PhpClass $class) + public function addProperties(PhpClass $class): PhpClass { $code = <<<'CODE' - class Stringifier extends SplStack implements JsonSerializable, ArrayAccessible + class Stringifier extends SplStack implements JsonSerializable, ArrayAccess { public const NAME = 'MyStringifier'; public const TYPE = 'ObjectStringifier'; @@ -97,7 +98,7 @@ class Stringifier extends SplStack implements JsonSerializable, ArrayAccessible /** * @test */ - public function fullBuild() + public function fullBuild(): PhpClass { $class = PhpClass::new('MyClass') ->addConst('KNOWN_TYPES', ['DYNAMIC', 'STATIC'], Modifier::PRIVATE) @@ -124,6 +125,7 @@ public function fullBuild() $class->emptyLine(); $class->append($method); + /** @var Comment $docBlock */ $docBlock = $class->getDocBlock(); $this->assertEquals( @@ -168,7 +170,7 @@ public function getErrors(): array * @test * @depends fullBuild */ - public function removeParts(PhpClass $class) + public function removeParts(PhpClass $class): PhpClass { $class ->unsetFinal() @@ -193,7 +195,7 @@ class Stringifier * @test * @depends removeParts */ - public function addAnotherParts(PhpClass $class) + public function addAnotherParts(PhpClass $class): void { $class->setAbstract(); $class->createDocBlock() diff --git a/tests/PhpFileTest.php b/tests/PhpFileTest.php index ae2a8c0..2705016 100644 --- a/tests/PhpFileTest.php +++ b/tests/PhpFileTest.php @@ -19,7 +19,7 @@ class PhpFileTest extends TestCase /** * @test */ - public function fullBuild() + public function fullBuild(): PhpFile { $file = PhpFile::new() ->setNamespace('App\Converter') @@ -110,7 +110,7 @@ private static function getPhpFile(): PhpFile * @test * @depends fullBuild */ - public function modifyFile(PhpFile $file) + public function modifyFile(PhpFile $file): PhpFile { $file->removeClass('ArrayConverter'); $file->removeUse('Symfony\Validator\Converters'); @@ -140,7 +140,7 @@ class YetAnotherClass * @test * @depends modifyFile */ - public function modifyComments(PhpFile $file) + public function modifyComments(PhpFile $file): PhpFile { $comment = $file->createComment($firstLine = 'THIS FILE WAS GENERATED.'); $comment->addLine($secondLine = 'DO NOT EDIT THIS FILE!'); @@ -154,6 +154,8 @@ public function modifyComments(PhpFile $file) $file->removeComment(); $file->setComment($firstLine); + + /** @var Comment $comment */ $comment = $file->getComment(); $this->assertEquals(<<getClass('YetAnotherClass'); - $class->createMethod('getArray') - ->append('return ', Collection::assoc(['status' => 200])); - $file->save($path, 0775); + if (null !== $class) { + $class->createMethod('getArray') + ->append('return ', Collection::assoc(['status' => 200])); + } + + $file->save($path); if (file_exists($path)) { require $path; } + // @phpstan-ignore-next-line $yetAnotherClass = new App\Converter\YetAnotherClass(); + // @phpstan-ignore-next-line $this->assertEquals(['status' => 200], $yetAnotherClass->getArray()); } } diff --git a/tests/PhpInterfaceTest.php b/tests/PhpInterfaceTest.php index ec52843..c6b74fa 100644 --- a/tests/PhpInterfaceTest.php +++ b/tests/PhpInterfaceTest.php @@ -6,6 +6,7 @@ use Murtukov\PHPCodeGenerator\ConverterInterface; use Murtukov\PHPCodeGenerator\Method; use Murtukov\PHPCodeGenerator\Modifier; +use Murtukov\PHPCodeGenerator\OOPStructure; use Murtukov\PHPCodeGenerator\PhpInterface; use PHPUnit\Framework\TestCase; @@ -14,7 +15,7 @@ class PhpInterfaceTest extends TestCase /** * @test */ - public function emptyBase() + public function emptyBase(): OOPStructure { $code = <<expectOutputString(<<expectOutputString(<<<'CODE' trait Stringifier @@ -56,7 +57,7 @@ trait Stringifier * @test * @depends addProperties */ - public function addMethodsAndDocBlock(PhpTrait $trait) + public function addMethodsAndDocBlock(PhpTrait $trait): PhpTrait { $trait->setDocBlock('This is just a test class.'); $trait->emptyLine(); @@ -103,7 +104,7 @@ public function getErrors(): array * @test * @depends addMethodsAndDocBlock */ - public function modifyTrait(PhpTrait $trait) + public function modifyTrait(PhpTrait $trait): void { $trait->clearContent(); $trait->createProperty('anotherProperty'); diff --git a/tests/PropertyTest.php b/tests/PropertyTest.php index 65f70d3..9884d26 100644 --- a/tests/PropertyTest.php +++ b/tests/PropertyTest.php @@ -11,7 +11,7 @@ class PropertyTest extends TestCase /** * @test */ - public function createProperty() + public function createProperty(): void { $property = Property::new($name = 'customProperty'); diff --git a/tests/UtilsTest.php b/tests/UtilsTest.php index cbf24eb..cd825da 100644 --- a/tests/UtilsTest.php +++ b/tests/UtilsTest.php @@ -10,7 +10,7 @@ class UtilsTest extends TestCase /** * @test */ - public function stringifyObject() + public function stringifyObject(): void { $object = new class() { public function __toString(): string @@ -29,7 +29,7 @@ public function __toString(): string /** * @test */ - public function skipNullValues() + public function skipNullValues(): void { Utils::$skipNullValues = true;