Skip to content

Commit

Permalink
Codebase: upgrade to PHP 8.0
Browse files Browse the repository at this point in the history
  • Loading branch information
f3l1x committed Jul 17, 2023
1 parent 529eb52 commit ed96ba0
Show file tree
Hide file tree
Showing 16 changed files with 272 additions and 441 deletions.
15 changes: 0 additions & 15 deletions .docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,6 @@ $multiplier->addCreateButton('Add 5', 5); // add five containers

### Macros

Latte 2

```latte
{form multiplier}
<div n:multiplier="multiplier">
<input n:name="text">
{btnRemove 'class' => 'myClass'}
</div>
{btnCreate multiplier class => myClass}
{btnCreate $form[multiplier]:5}
{/form}
```

Latte 3

```latte
{form multiplier}
<div n:multiplier="multiplier">
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"multiplier"
],
"require": {
"php": ">=7.2",
"php": ">=8.0",
"nette/forms": "^3.1.0"
},
"require-dev": {
Expand Down
4 changes: 0 additions & 4 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,3 @@ parameters:
paths:
- src
- .docs

excludePaths:
analyse:
- src/Macros/MultiplierMacros.php
14 changes: 6 additions & 8 deletions src/Buttons/CreateButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,18 @@ final class CreateButton

use SmartObject;

/** @var string|null */
private $caption;
/** @var callable[] */
public array $onCreate = [];

/** @var int */
private $copyCount;
private ?string $caption = null;

/** @var callable[] */
public $onCreate = [];
private int $copyCount;

/** @var mixed[]|null */
private $validationScope = null;
private ?array $validationScope = null;

/** @var string[] */
private $classes = [];
private array $classes = [];

public function __construct(?string $caption, int $copyCount = 1)
{
Expand Down
9 changes: 4 additions & 5 deletions src/Buttons/RemoveButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@ final class RemoveButton

use SmartObject;

/** @var string|null */
private $caption;

/** @var callable[] */
public $onCreate = [];
public array $onCreate = [];

private ?string $caption = null;

/** @var string[] */
private $classes = [];
private array $classes = [];

public function __construct(?string $caption)
{
Expand Down
36 changes: 12 additions & 24 deletions src/ComponentResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,31 @@

namespace Contributte\FormMultiplier;

use Nette\Utils\Strings;

final class ComponentResolver
{

/** @var string|int|null */
private $removeId;
private string|int|null $removeId = null;

/** @var bool */
private $removeAction = false;
private bool $removeAction = false;

/** @var bool */
private $createAction = false;
private bool $createAction = false;

/** @var int */
private $createNum = 1;
private int $createNum = 1;

/** @var mixed[] */
private $httpData = [];
private array $httpData = [];

/** @var int|null */
private $maxCopies;
private ?int $maxCopies = null;

/** @var mixed[] */
private $purgedHttpData;
private ?array $purgedHttpData = null;

/** @var mixed[] */
private $defaults;
private array $defaults = [];

/** @var int */
private $minCopies;
private int $minCopies;

/** @var bool */
private $reached = false;
private bool $reached = false;

/**
* @param mixed[] $httpData
Expand All @@ -49,7 +40,7 @@ public function __construct(array $httpData, array $defaults, ?int $maxCopies, i
$this->minCopies = $minCopies;

foreach ($httpData as $index => $_) {
if (Strings::startsWith((string) $index, Multiplier::SUBMIT_CREATE_NAME)) {
if (str_starts_with((string) $index, Multiplier::SUBMIT_CREATE_NAME)) {
$this->createAction = true;
$num = substr($index, 18);
if ($num) {
Expand Down Expand Up @@ -131,10 +122,7 @@ public function isRemoveAction(): bool
return $this->removeAction;
}

/**
* @return int|string|null
*/
public function getRemoveId()
public function getRemoveId(): int|string|null
{
return $this->removeId;
}
Expand Down
12 changes: 3 additions & 9 deletions src/DI/MultiplierExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

namespace Contributte\FormMultiplier\DI;

use Contributte\FormMultiplier\Macros\MultiplierMacros;
use Contributte\FormMultiplier\Latte\Extension\MultiplierExtension as LatteMultiplierExtension;
use Contributte\FormMultiplier\Multiplier;
use Latte\Engine;
use Nette\Bridges\ApplicationLatte\LatteFactory;
use Nette\DI\CompilerExtension;
use Nette\DI\Definitions\FactoryDefinition;
Expand Down Expand Up @@ -35,18 +34,13 @@ public function beforeCompile(): void
sprintf(
'latte.latteFactory service definition must be of type %s, not %s',
FactoryDefinition::class,
get_class($factory)
$factory::class
)
);
}

$resultDefinition = $factory->getResultDefinition();

if (version_compare(Engine::VERSION, '3', '<')) { // @phpstan-ignore-line
$resultDefinition->addSetup(MultiplierMacros::class . '::install(?->getCompiler())', ['@self']);
} else {
$resultDefinition->addSetup('addExtension', [new Statement(\Contributte\FormMultiplier\Latte\Extension\MultiplierExtension::class)]);
}
$resultDefinition->addSetup('addExtension', [new Statement(LatteMultiplierExtension::class)]);
}

public function afterCompile(ClassType $class): void
Expand Down
27 changes: 9 additions & 18 deletions src/Latte/Extension/Node/MultiplierAddNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,15 @@
use Latte\Compiler\Nodes\StatementNode;
use Latte\Compiler\PrintContext;
use Latte\Compiler\Tag;
use LogicException;
use Nette\ComponentModel\IComponent;
use Nette\Forms\Container;
use Nette\Forms\SubmitterControl;

final class MultiplierAddNode extends StatementNode
{

/** @var ExpressionNode */
public $name;
public ExpressionNode $name;

/** @var ArrayNode */
public $attributes;
public ArrayNode $attributes;

/** @var ExpressionNode */
public $part;
public ExpressionNode $part;

public static function create(Tag $tag): self
{
Expand All @@ -47,6 +40,11 @@ public static function create(Tag $tag): self
return $node;
}

public static function getCreateButton(Multiplier $multiplier, int|string $buttonId): ?Submitter
{
return $multiplier->getCreateButtons()[$buttonId] ?? null;
}

public function print(PrintContext $context): string
{
return $context->format(
Expand All @@ -66,18 +64,11 @@ public function print(PrintContext $context): string
);
}

/**
* @param int|string $buttonId
*/
public static function getCreateButton(Multiplier $multiplier, $buttonId): ?Submitter
{
return $multiplier->getCreateButtons()[$buttonId] ?? null;
}

public function &getIterator(): \Generator
{
yield $this->name;
yield $this->attributes;
yield $this->part;
}

}
10 changes: 4 additions & 6 deletions src/Latte/Extension/Node/MultiplierNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,15 @@
use Latte\Compiler\Nodes\Php\ExpressionNode;
use Latte\Compiler\Nodes\Php\Scalar\StringNode;
use Latte\Compiler\Nodes\StatementNode;
use Latte\Compiler\Position;
use Latte\Compiler\PrintContext;
use Latte\Compiler\Tag;

final class MultiplierNode extends StatementNode
{

/** @var ExpressionNode */
public $name;
public ExpressionNode $name;

/** @var AreaNode */
public $content;
public AreaNode $content;

/**
* @return Generator<int, ?mixed[], array{AreaNode, ?Tag}, self>
Expand All @@ -28,7 +25,7 @@ public static function create(Tag $tag): Generator
$tag->outputMode = $tag::OutputRemoveIndentation;
$tag->expectArguments();

$node = new static;
$node = new static();
$node->name = $tag->parser->parseUnquotedStringOrExpression();

[$node->content] = yield;
Expand Down Expand Up @@ -57,6 +54,7 @@ public function print(PrintContext $context): string
$this->content
);
}

public function &getIterator(): \Generator
{
yield $this->name;
Expand Down
32 changes: 15 additions & 17 deletions src/Latte/Extension/Node/MultiplierRemoveNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@
use LogicException;
use Nette\ComponentModel\IComponent;
use Nette\Forms\Container;
use Nette\Forms\SubmitterControl;

final class MultiplierRemoveNode extends StatementNode
{

/** @var ArrayNode */
public $attributes;
public ArrayNode $attributes;

public static function create(Tag $tag): self
{
Expand All @@ -26,20 +24,6 @@ public static function create(Tag $tag): self
return $node;
}

public function print(PrintContext $context): string
{
return $context->format(
'if ($ʟ_input = %raw::getRemoveButton($this->global->formsStack)) {'
. 'echo $ʟ_input->getControl()'
. ($this->attributes->items ? '->addAttributes(%1.node)' : '')
. ';'
. '} %2.line',
self::class,
$this->attributes,
$this->position
);
}

/**
* @param Container[] $formsStack
*/
Expand All @@ -54,6 +38,20 @@ public static function getRemoveButton(array $formsStack): ?IComponent
return $container->getComponent(Multiplier::SUBMIT_REMOVE_NAME, false);
}

public function print(PrintContext $context): string
{
return $context->format(
'if ($ʟ_input = %raw::getRemoveButton($this->global->formsStack)) {'
. 'echo $ʟ_input->getControl()'
. ($this->attributes->items ? '->addAttributes(%1.node)' : '')
. ';'
. '} %2.line',
self::class,
$this->attributes,
$this->position
);
}

public function &getIterator(): \Generator
{
yield $this->attributes;
Expand Down
Loading

0 comments on commit ed96ba0

Please sign in to comment.