diff --git a/composer.json b/composer.json index f0b17e8..f9c45e6 100644 --- a/composer.json +++ b/composer.json @@ -18,13 +18,13 @@ "Memio\\TwigTemplateEngine\\Config\\": "config" }}, "require": { - "memio/model": "^2.0", - "memio/pretty-printer": "^2.0", - "php": "^7.0", + "memio/model": "^3.0", + "memio/pretty-printer": "^3.0", + "php": "^7.2 || ^8.0", "twig/twig": "^1.18 || ^2.0 || ^3.0" }, "require-dev": { "friendsofphp/php-cs-fixer": "^2.10", - "phpspec/phpspec": "^4.3" + "phpspec/phpspec": "^6.1 || ^7.0" } } diff --git a/spec/Memio/TwigTemplateEngine/TwigExtension/Line/ContractLineStrategySpec.php b/spec/Memio/TwigTemplateEngine/TwigExtension/Line/ContractLineStrategySpec.php index 01e01cd..939c5da 100644 --- a/spec/Memio/TwigTemplateEngine/TwigExtension/Line/ContractLineStrategySpec.php +++ b/spec/Memio/TwigTemplateEngine/TwigExtension/Line/ContractLineStrategySpec.php @@ -11,30 +11,34 @@ namespace spec\Memio\TwigTemplateEngine\TwigExtension\Line; +use Memio\Model\Constant; use Memio\Model\Contract; +use Memio\Model\Method; use Memio\TwigTemplateEngine\TwigExtension\Line\LineStrategy; +use Memio\TwigTemplateEngine\TwigExtension\Line\ContractLineStrategy; use PhpSpec\ObjectBehavior; class ContractLineStrategySpec extends ObjectBehavior { - const CONSTANT_BLOCK = 'constants'; - function it_is_a_line_strategy() { $this->shouldImplement(LineStrategy::class); } - function it_supports_contracts(Contract $contract) + function it_supports_contracts() { + $contract = (new Contract('Memio\PrettyPrinter\TemplateEngine')); + $this->supports($contract)->shouldBe(true); } - function it_needs_line_after_constants_if_contract_has_both_constants_and_methods( - Contract $contract - ) { - $contract->allConstants()->willReturn([1]); - $contract->allMethods()->willReturn([2]); + function it_needs_an_empty_line_after_constants_if_it_also_has_methods() + { + $contract = (new Contract('Memio\PrettyPrinter\TemplateEngine')) + ->addConstant(new Constant('CONSTANT_ONE', 1)) + ->addMethod(new Method('methodOne')) + ; - $this->needsLineAfter($contract, self::CONSTANT_BLOCK)->shouldBe(true); + $this->needsLineAfter($contract, ContractLineStrategy::CONSTANTS_BLOCK)->shouldBe(true); } } diff --git a/spec/Memio/TwigTemplateEngine/TwigExtension/Line/FileLineStrategySpec.php b/spec/Memio/TwigTemplateEngine/TwigExtension/Line/FileLineStrategySpec.php index a1e6d8b..9e65398 100644 --- a/spec/Memio/TwigTemplateEngine/TwigExtension/Line/FileLineStrategySpec.php +++ b/spec/Memio/TwigTemplateEngine/TwigExtension/Line/FileLineStrategySpec.php @@ -12,28 +12,31 @@ namespace spec\Memio\TwigTemplateEngine\TwigExtension\Line; use Memio\Model\File; +use Memio\Model\FullyQualifiedName; +use Memio\TwigTemplateEngine\TwigExtension\Line\FileLineStrategy; use Memio\TwigTemplateEngine\TwigExtension\Line\LineStrategy; use PhpSpec\ObjectBehavior; class FileLineStrategySpec extends ObjectBehavior { - const IMPORT_BLOCK = 'fully_qualified_names'; - function it_is_a_line_strategy() { $this->shouldImplement(LineStrategy::class); } - function it_supports_files(File $file) + function it_supports_files() { + $file = new File('src/Memio/Model/Contract.php'); + $this->supports($file)->shouldBe(true); } - function it_needs_line_after_fully_qualified_names_if_file_has_fully_qualified_names( - File $file - ) { - $file->allFullyQualifiedNames()->willReturn([1]); + function it_needs_an_empty_line_after_use_statements() + { + $file = (new File('src/Memio/Model/Contract.php')) + ->addFullyQualifiedName(new FullyQualifiedName('Memio\Model\Phpdoc\StructurePhpdoc')) + ; - $this->needsLineAfter($file, self::IMPORT_BLOCK)->shouldBe(true); + $this->needsLineAfter($file, FileLineStrategy::USE_STATEMENTS_BLOCK)->shouldBe(true); } } diff --git a/spec/Memio/TwigTemplateEngine/TwigExtension/Line/LineSpec.php b/spec/Memio/TwigTemplateEngine/TwigExtension/Line/LineSpec.php index 618f5e1..6053946 100644 --- a/spec/Memio/TwigTemplateEngine/TwigExtension/Line/LineSpec.php +++ b/spec/Memio/TwigTemplateEngine/TwigExtension/Line/LineSpec.php @@ -18,32 +18,32 @@ class LineSpec extends ObjectBehavior { - const BLOCK = 'constants'; - const STRATEGY_RETURN = true; - - function let(LineStrategy $lineStrategy) - { - $this->add($lineStrategy); - } - function it_executes_the_first_strategy_that_supports_given_model( - Argument $model, LineStrategy $lineStrategy ) { + $this->add($lineStrategy); + + $model = new Argument('string', 'filename'); + $block = 'arguments'; + $strategyReturn = true; + $lineStrategy->supports($model)->willReturn(true); - $lineStrategy->needsLineAfter($model, self::BLOCK)->willReturn(self::STRATEGY_RETURN); + $lineStrategy->needsLineAfter($model, $block)->willReturn($strategyReturn); - $this->needsLineAfter($model, self::BLOCK)->shouldBe(self::STRATEGY_RETURN); + $this->needsLineAfter($model, $block)->shouldBe($strategyReturn); } function it_fails_when_no_strategy_supports_given_model( - Argument $model, LineStrategy $lineStrategy ) { + $this->add($lineStrategy); + + $model = new Argument('string', 'filename'); + $lineStrategy->supports($model)->willReturn(false); $this->shouldThrow( InvalidArgumentException::class - )->duringNeedsLineAfter($model, self::BLOCK); + )->duringNeedsLineAfter($model, 'arguments'); } } diff --git a/spec/Memio/TwigTemplateEngine/TwigExtension/Line/MethodPhpdocLineStrategySpec.php b/spec/Memio/TwigTemplateEngine/TwigExtension/Line/MethodPhpdocLineStrategySpec.php index 8ae2de2..b8d3c5f 100644 --- a/spec/Memio/TwigTemplateEngine/TwigExtension/Line/MethodPhpdocLineStrategySpec.php +++ b/spec/Memio/TwigTemplateEngine/TwigExtension/Line/MethodPhpdocLineStrategySpec.php @@ -26,52 +26,39 @@ function it_is_a_line_strategy() $this->shouldImplement(LineStrategy::class); } - function it_supports_method_phpdocs(MethodPhpdoc $methodPhpdoc) + function it_supports_method_phpdocs() { + $methodPhpdoc = new MethodPhpdoc(); + $this->supports($methodPhpdoc)->shouldBe(true); } - function it_needs_line_after_description_if_it_has_any_other_tag( - Description $description, - DeprecationTag $deprecationTag, - MethodPhpdoc $methodPhpdoc - ) { - $methodPhpdoc->getApiTag()->willReturn(null); - $methodPhpdoc->getDescription()->willReturn($description); - $methodPhpdoc->getDeprecationTag()->willReturn($deprecationTag); - $methodPhpdoc->getParameterTags()->willReturn([]); - $methodPhpdoc->getReturnTag()->willReturn(null); - $methodPhpdoc->getThrowTags()->willReturn([]); + function it_needs_a_new_line_after_description_if_it_has_any_other_tag() + { + $methodPhpdoc = (new MethodPhpdoc()) + ->setDescription(new Description('Helpful description')) + ->setDeprecationTag(new DeprecationTag()) + ; $this->needsLineAfter($methodPhpdoc, 'description')->shouldBe(true); } - function it_needs_line_after_parameter_tags_if_it_has_api_or_deprecation_tags( - DeprecationTag $deprecationTag, - MethodPhpdoc $methodPhpdoc, - ParameterTag $parameterTag - ) { - $methodPhpdoc->getApiTag()->willReturn(null); - $methodPhpdoc->getDescription()->willReturn(null); - $methodPhpdoc->getDeprecationTag()->willReturn($deprecationTag); - $methodPhpdoc->getParameterTags()->willReturn([$parameterTag]); - $methodPhpdoc->getReturnTag()->willReturn(null); - $methodPhpdoc->getThrowTags()->willReturn([]); + function it_needs_a_new_line_after_parameter_tags_if_it_has_a_deprecation_tag() + { + $methodPhpdoc = (new MethodPhpdoc()) + ->addParameterTag(new ParameterTag('string', 'filename')) + ->setDeprecationTag(new DeprecationTag()) + ; $this->needsLineAfter($methodPhpdoc, 'parameter_tags')->shouldBe(true); } - function it_needs_line_after_deprecation_it_also_has_an_api_tag( - ApiTag $apiTag, - DeprecationTag $deprecationTag, - MethodPhpdoc $methodPhpdoc - ) { - $methodPhpdoc->getDeprecationTag()->willReturn($deprecationTag); - $methodPhpdoc->getDescription()->willReturn(null); - $methodPhpdoc->getApiTag()->willReturn($apiTag); - $methodPhpdoc->getParameterTags()->willReturn([]); - $methodPhpdoc->getReturnTag()->willReturn(null); - $methodPhpdoc->getThrowTags()->willReturn([]); + function it_needs_a_new_line_after_deprecation_if_it_also_has_an_api_tag() + { + $methodPhpdoc = (new MethodPhpdoc()) + ->setDeprecationTag(new DeprecationTag()) + ->setApiTag(new ApiTag()) + ; $this->needsLineAfter($methodPhpdoc, 'deprecation_tag')->shouldBe(true); } diff --git a/spec/Memio/TwigTemplateEngine/TwigExtension/Line/ObjectLineStrategySpec.php b/spec/Memio/TwigTemplateEngine/TwigExtension/Line/ObjectLineStrategySpec.php index 9172669..cd4b0fc 100644 --- a/spec/Memio/TwigTemplateEngine/TwigExtension/Line/ObjectLineStrategySpec.php +++ b/spec/Memio/TwigTemplateEngine/TwigExtension/Line/ObjectLineStrategySpec.php @@ -12,50 +12,55 @@ namespace spec\Memio\TwigTemplateEngine\TwigExtension\Line; use Memio\Model\Objekt; +use Memio\Model\Constant; +use Memio\Model\Method; +use Memio\Model\Property; use Memio\TwigTemplateEngine\TwigExtension\Line\LineStrategy; +use Memio\TwigTemplateEngine\TwigExtension\Line\ObjectLineStrategy; use PhpSpec\ObjectBehavior; class ObjectLineStrategySpec extends ObjectBehavior { - const CONSTANT_BLOCK = 'constants'; - const PROPERTY_BLOCK = 'properties'; - function it_is_a_line_strategy() { $this->shouldImplement(LineStrategy::class); } - function it_supports_objects(Objekt $object) + function it_supports_objects() { - $this->supports($object)->shouldBe(true); + $objekt = new Objekt('Memio\Model\Objekt'); + + $this->supports($objekt)->shouldBe(true); } - function it_needs_line_after_constants_if_object_has_both_constants_and_properties( - Objekt $object - ) { - $object->allConstants()->willReturn([1]); - $object->allProperties()->willReturn([2]); - $object->allMethods()->willReturn([]); + function it_needs_an_empty_line_after_constants_if_it_also_has_properties() + { + $objekt = (new Objekt('Memio\Model\Objekt')) + ->addConstant(new Constant('CONSTANT_ONE', 1)) + ->addProperty(new Property('filename')) + ->addProperty(new Property('content')) + ; - $this->needsLineAfter($object, self::CONSTANT_BLOCK)->shouldBe(true); + $this->needsLineAfter($objekt, ObjectLineStrategy::CONSTANTS_BLOCK)->shouldBe(true); } - function it_needs_line_after_constants_if_object_has_both_constants_and_methods( - Objekt $object - ) { - $object->allConstants()->willReturn([1]); - $object->allProperties()->willReturn([]); - $object->allMethods()->willReturn([2]); + function it_needs_an_empty_line_after_constants_if_it_also_has_methods() + { + $objekt = (new Objekt('Memio\Model\Objekt')) + ->addConstant(new Constant('CONSTANT_ONE', 1)) + ->addMethod(new Method('write')) + ; - $this->needsLineAfter($object, self::CONSTANT_BLOCK)->shouldBe(true); + $this->needsLineAfter($objekt, ObjectLineStrategy::CONSTANTS_BLOCK)->shouldBe(true); } - function it_needs_line_after_properties_if_object_has_both_properties_and_methods(Objekt $object) + function it_needs_an_empty_line_after_properties_if_it_also_has_methods() { - $object->allConstants()->willReturn([]); - $object->allProperties()->willReturn([1]); - $object->allMethods()->willReturn([2]); + $objekt = (new Objekt('Memio\Model\Objekt')) + ->addProperty(new Property('filename')) + ->addMethod(new Method('write')) + ; - $this->needsLineAfter($object, self::PROPERTY_BLOCK)->shouldBe(true); + $this->needsLineAfter($objekt, ObjectLineStrategy::PROPERTIES_BLOCK)->shouldBe(true); } } diff --git a/spec/Memio/TwigTemplateEngine/TwigExtension/Line/StructurePhpdocLineStrategySpec.php b/spec/Memio/TwigTemplateEngine/TwigExtension/Line/StructurePhpdocLineStrategySpec.php index a70c7bd..0bca958 100644 --- a/spec/Memio/TwigTemplateEngine/TwigExtension/Line/StructurePhpdocLineStrategySpec.php +++ b/spec/Memio/TwigTemplateEngine/TwigExtension/Line/StructurePhpdocLineStrategySpec.php @@ -16,6 +16,7 @@ use Memio\Model\Phpdoc\Description; use Memio\Model\Phpdoc\DeprecationTag; use Memio\TwigTemplateEngine\TwigExtension\Line\LineStrategy; +use Memio\TwigTemplateEngine\TwigExtension\Line\StructurePhpdocLineStrategy; use PhpSpec\ObjectBehavior; class StructurePhpdocLineStrategySpec extends ObjectBehavior @@ -25,34 +26,40 @@ function it_is_a_line_strategy() $this->shouldImplement(LineStrategy::class); } - function it_supports_structure_phpdocs(StructurePhpdoc $structurePhpdoc) + function it_supports_structure_phpdocs() { + $structurePhpdoc = new StructurePhpdoc(); + $this->supports($structurePhpdoc)->shouldBe(true); } - function it_needs_line_after_description_if_description_and_deprecation_or_api_are_defined( - ApiTag $apiTag, - Description $description, - DeprecationTag $deprecationTag, - StructurePhpdoc $structurePhpdoc - ) { - $structurePhpdoc->getApiTag()->willReturn($apiTag); - $structurePhpdoc->getDescription()->willReturn($description); - $structurePhpdoc->getDeprecationTag()->willReturn($deprecationTag); - - $this->needsLineAfter($structurePhpdoc, 'description')->shouldBe(true); + function it_needs_an_empty_line_after_description_if_it_also_has_an_api_tag() + { + $structurePhpdoc = (new StructurePhpdoc()) + ->setDescription(new Description('helpful description')) + ->setApiTag(new ApiTag()) + ; + + $this->needsLineAfter($structurePhpdoc, StructurePhpdocLineStrategy::DESCRPTION)->shouldBe(true); } - function it_needs_line_after_deprecation_if_deprecation_and_api_are_defined( - ApiTag $apiTag, - Description $description, - DeprecationTag $deprecationTag, - StructurePhpdoc $structurePhpdoc - ) { - $structurePhpdoc->getApiTag()->willReturn($apiTag); - $structurePhpdoc->getDescription()->willReturn($description); - $structurePhpdoc->getDeprecationTag()->willReturn($deprecationTag); - - $this->needsLineAfter($structurePhpdoc, 'deprecation_tag')->shouldBe(true); + function it_needs_an_empty_line_after_description_if_it_also_has_a_deprecation_tag() + { + $structurePhpdoc = (new StructurePhpdoc()) + ->setDescription(new Description('helpful description')) + ->setDeprecationTag(new DeprecationTag()) + ; + + $this->needsLineAfter($structurePhpdoc, StructurePhpdocLineStrategy::DESCRPTION)->shouldBe(true); + } + + function it_needs_an_empty_line_after_deprecation_tag_if_it_also_has_an_api_tag() + { + $structurePhpdoc = (new StructurePhpdoc()) + ->setDeprecationTag(new DeprecationTag()) + ->setApiTag(new ApiTag()) + ; + + $this->needsLineAfter($structurePhpdoc, StructurePhpdocLineStrategy::DEPRECATION_TAG)->shouldBe(true); } } diff --git a/src/Memio/TwigTemplateEngine/TwigExtension/Line/ContractLineStrategy.php b/src/Memio/TwigTemplateEngine/TwigExtension/Line/ContractLineStrategy.php index 067d46f..49f50ba 100644 --- a/src/Memio/TwigTemplateEngine/TwigExtension/Line/ContractLineStrategy.php +++ b/src/Memio/TwigTemplateEngine/TwigExtension/Line/ContractLineStrategy.php @@ -16,6 +16,8 @@ class ContractLineStrategy implements LineStrategy { + const CONSTANTS_BLOCK = 'constants'; + public function supports($model): bool { return $model instanceof Contract; @@ -23,10 +25,8 @@ public function supports($model): bool public function needsLineAfter($model, string $block): bool { - $constants = $model->allConstants(); - $methods = $model->allMethods(); - if ('constants' === $block) { - return !empty($constants) && !empty($methods); + if (self::CONSTANTS_BLOCK === $block) { + return [] !== $model->constants && [] !== $model->methods; } throw new InvalidArgumentException('The function needs_line_after does not support given "'.$block.'"'); diff --git a/src/Memio/TwigTemplateEngine/TwigExtension/Line/FileLineStrategy.php b/src/Memio/TwigTemplateEngine/TwigExtension/Line/FileLineStrategy.php index 070ffd4..bfb28e6 100644 --- a/src/Memio/TwigTemplateEngine/TwigExtension/Line/FileLineStrategy.php +++ b/src/Memio/TwigTemplateEngine/TwigExtension/Line/FileLineStrategy.php @@ -16,6 +16,8 @@ class FileLineStrategy implements LineStrategy { + const USE_STATEMENTS_BLOCK = 'fully_qualified_names'; + public function supports($model): bool { return $model instanceof File; @@ -23,9 +25,8 @@ public function supports($model): bool public function needsLineAfter($model, string $block): bool { - $fullyQualifiedNames = $model->allFullyQualifiedNames(); - if ('fully_qualified_names' === $block) { - return !empty($fullyQualifiedNames); + if (self::USE_STATEMENTS_BLOCK === $block) { + return [] !== $model->fullyQualifiedNames; } throw new InvalidArgumentException('The function needs_line_after does not support given "'.$block.'"'); diff --git a/src/Memio/TwigTemplateEngine/TwigExtension/Line/MethodPhpdocLineStrategy.php b/src/Memio/TwigTemplateEngine/TwigExtension/Line/MethodPhpdocLineStrategy.php index 9b29503..0217fbe 100644 --- a/src/Memio/TwigTemplateEngine/TwigExtension/Line/MethodPhpdocLineStrategy.php +++ b/src/Memio/TwigTemplateEngine/TwigExtension/Line/MethodPhpdocLineStrategy.php @@ -22,15 +22,12 @@ public function supports($model): bool public function needsLineAfter($model, string $block): bool { - $parameterTags = $model->getParameterTags(); - $throwTags = $model->getThrowTags(); - - $hasApiTag = (null !== $model->getApiTag()); - $hasParameterTags = (!empty($parameterTags)); - $hasDescription = (null !== $model->getDescription()); - $hasDeprecationTag = (null !== $model->getDeprecationTag()); - $hasReturnTag = (null !== $model->getReturnTag()); - $hasThrowTags = (!empty($throwTags)); + $hasApiTag = (null !== $model->apiTag); + $hasParameterTags = ([] !== $model->parameterTags); + $hasDescription = (null !== $model->description); + $hasDeprecationTag = (null !== $model->deprecationTag); + $hasReturnTag = (null !== $model->returnTag); + $hasThrowTags = ([] !== $model->throwTags); if ('description' === $block) { return $hasDescription && ($hasReturnTag || $hasApiTag || $hasDeprecationTag || $hasParameterTags || $hasThrowTags); diff --git a/src/Memio/TwigTemplateEngine/TwigExtension/Line/ObjectLineStrategy.php b/src/Memio/TwigTemplateEngine/TwigExtension/Line/ObjectLineStrategy.php index dc449f3..f38b164 100644 --- a/src/Memio/TwigTemplateEngine/TwigExtension/Line/ObjectLineStrategy.php +++ b/src/Memio/TwigTemplateEngine/TwigExtension/Line/ObjectLineStrategy.php @@ -16,6 +16,9 @@ class ObjectLineStrategy implements LineStrategy { + const CONSTANTS_BLOCK = 'constants'; + const PROPERTIES_BLOCK = 'properties'; + public function supports($model): bool { return $model instanceof Objekt; @@ -23,14 +26,11 @@ public function supports($model): bool public function needsLineAfter($model, string $block): bool { - $constants = $model->allConstants(); - $properties = $model->allProperties(); - $methods = $model->allMethods(); - if ('constants' === $block) { - return !empty($constants) && (!empty($properties) || !empty($methods)); + if (self::CONSTANTS_BLOCK === $block) { + return [] !== $model->constants && ([] !== $model->properties || [] !== $model->methods); } - if ('properties' === $block) { - return !empty($properties) && !empty($methods); + if (self::PROPERTIES_BLOCK === $block) { + return [] !== $model->properties && [] !== $model->methods; } throw new InvalidArgumentException('The function needs_line_after does not support given "'.$block.'"'); diff --git a/src/Memio/TwigTemplateEngine/TwigExtension/Line/StructurePhpdocLineStrategy.php b/src/Memio/TwigTemplateEngine/TwigExtension/Line/StructurePhpdocLineStrategy.php index dc5f04b..207d17d 100644 --- a/src/Memio/TwigTemplateEngine/TwigExtension/Line/StructurePhpdocLineStrategy.php +++ b/src/Memio/TwigTemplateEngine/TwigExtension/Line/StructurePhpdocLineStrategy.php @@ -15,6 +15,9 @@ class StructurePhpdocLineStrategy implements LineStrategy { + const DESCRPTION = 'description'; + const DEPRECATION_TAG = 'deprecation_tag'; + public function supports($model): bool { return $model instanceof StructurePhpdoc; @@ -22,13 +25,14 @@ public function supports($model): bool public function needsLineAfter($model, string $block): bool { - $hasDescription = (null !== $model->getDescription()); - $hasApiTag = (null !== $model->getApiTag()); - $hasDeprecationTag = (null !== $model->getDeprecationTag()); - if ('description' === $block) { + $hasDescription = (null !== $model->description); + $hasApiTag = (null !== $model->apiTag); + $hasDeprecationTag = (null !== $model->deprecationTag); + + if (self::DESCRPTION === $block) { return $hasDescription && ($hasApiTag || $hasDeprecationTag); } - if ('deprecation_tag' === $block) { + if (self::DEPRECATION_TAG === $block) { return $hasApiTag && $hasDeprecationTag; } }