Skip to content

Commit

Permalink
Added php 8 and phpspec 7 support, dropped support for php >7.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Loïc Faugeron committed Aug 13, 2024
1 parent 273d041 commit 55b30b2
Show file tree
Hide file tree
Showing 12 changed files with 151 additions and 143 deletions.
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
26 changes: 13 additions & 13 deletions spec/Memio/TwigTemplateEngine/TwigExtension/Line/LineSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@

class ContractLineStrategy implements LineStrategy
{
const CONSTANTS_BLOCK = 'constants';

public function supports($model): bool
{
return $model instanceof Contract;
}

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.'"');
Expand Down
Loading

0 comments on commit 55b30b2

Please sign in to comment.