Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating Reactor code with PHP 8.1 features #638

Merged
merged 4 commits into from
Apr 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
added return type `mixed` to the method `getContext` in `Spiral\Filters\FilterInterface` interface.
Added return type `mixed` to the method `getValue` in `Spiral\Filters\InputInterface`.
- [spiral/http] Config `Spiral\Config\JsonPayloadConfig` moved to the `Spiral\Bootloader\Http\JsonPayloadConfig`.
- [spiral/reactor] Added return type `mixed` and `array|string` parameter type of `$search`,
`array|string` parameter type of `$replace` to the method `replace` in `Spiral\Reactor\ReplaceableInterface`.
- [spiral/session] Added return type `void` to the method `resume` in `Spiral\Session\SessionInterface`.
- [spiral/session] Added return type `self` and `mixed` parameter type of `$value` to the method `set`
in `Spiral\Session\SessionSectionInterface`.
Expand Down
9 changes: 1 addition & 8 deletions src/Reactor/src/AbstractDeclaration.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Reactor;
Expand All @@ -25,6 +18,6 @@ abstract class AbstractDeclaration implements DeclarationInterface

protected function addIndent(string $string, int $indent = 0): string
{
return str_repeat(self::INDENT, max($indent, 0)) . $string;
return \str_repeat(self::INDENT, \max($indent, 0)) . $string;
}
}
89 changes: 24 additions & 65 deletions src/Reactor/src/Aggregator.php
Original file line number Diff line number Diff line change
@@ -1,57 +1,36 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Reactor;

use ArrayAccess;
use ArrayIterator;
use Countable;
use IteratorAggregate;
use ReflectionObject;
use Spiral\Reactor\Exception\ReactorException;

/**
* Provides ability to aggregate specific set of elements (type constrained), render them or
* apply set of operations.
*/
class Aggregator extends AbstractDeclaration implements
ArrayAccess,
IteratorAggregate,
Countable,
\ArrayAccess,
\IteratorAggregate,
\Countable,
ReplaceableInterface
{
/**
* @var array
*/
private $allowed;

/**
* @var DeclarationInterface[]
* @param DeclarationInterface[] $elements
*/
private $elements;

public function __construct(array $allowed, array $elements = [])
{
$this->allowed = $allowed;
$this->elements = $elements;
public function __construct(
private array $allowed,
private array $elements = []
) {
}

/**
* Get element by it's name.
*
* @param string $name
* @return DeclarationInterface
* @throws ReactorException
*/
public function __get($name)
public function __get($name): DeclarationInterface
{
return $this->get($name);
}
Expand Down Expand Up @@ -87,19 +66,19 @@ public function has(string $name): bool
*/
public function add(DeclarationInterface $element): self
{
$reflector = new ReflectionObject($element);
$reflector = new \ReflectionObject($element);

$allowed = false;
foreach ($this->allowed as $class) {
if ($reflector->isSubclassOf($class) || get_class($element) === $class) {
if ($reflector->isSubclassOf($class) || $element::class === $class) {
$allowed = true;
break;
}
}

if (!$allowed) {
$type = get_class($element);
throw new ReactorException("Elements with type '{$type}' are not allowed");
$type = $element::class;
throw new ReactorException(\sprintf("Elements with type '%s' are not allowed", $type));
}

$this->elements[] = $element;
Expand All @@ -110,10 +89,9 @@ public function add(DeclarationInterface $element): self
/**
* Get named element by it's name.
*
* @return DeclarationInterface
* @throws ReactorException
*/
public function get(string $name)
public function get(string $name): DeclarationInterface
{
return $this->find($name);
}
Expand All @@ -133,50 +111,34 @@ public function remove(string $name): self
}

/**
* @return ArrayIterator<array-key, DeclarationInterface>
* @return \ArrayIterator<array-key, DeclarationInterface>
*/
public function getIterator(): ArrayIterator
public function getIterator(): \ArrayIterator
{
return new ArrayIterator($this->elements);
return new \ArrayIterator($this->elements);
}

/**
* {@inheritdoc}
*/
public function offsetExists($offset): bool
public function offsetExists(mixed $offset): bool
{
return $this->has($offset);
}

/**
* {@inheritdoc}
*/
#[\ReturnTypeWillChange]
public function offsetGet($offset)
public function offsetGet(mixed $offset): mixed
{
return $this->get($offset);
}

/**
* {@inheritdoc}
*/
public function offsetSet($offset, $value): void
public function offsetSet(mixed $offset, mixed $value): void
{
$this->remove($offset)->add($value);
}

/**
* {@inheritdoc}
*/
public function offsetUnset($offset): void
public function offsetUnset(mixed $offset): void
{
$this->remove($offset);
}

/**
* {@inheritdoc}
*/
public function replace($search, $replace): Aggregator
public function replace(array|string $search, array|string $replace): Aggregator
{
foreach ($this->elements as $element) {
if ($element instanceof ReplaceableInterface) {
Expand All @@ -187,9 +149,6 @@ public function replace($search, $replace): Aggregator
return $this;
}

/**
* {@inheritdoc}
*/
public function render(int $indentLevel = 0): string
{
$result = '';
Expand All @@ -198,7 +157,7 @@ public function render(int $indentLevel = 0): string
$result .= $element->render($indentLevel) . "\n\n";
}

return rtrim($result, "\n");
return \rtrim($result, "\n");
}

/**
Expand All @@ -214,6 +173,6 @@ protected function find(string $name): DeclarationInterface
}
}

throw new ReactorException("Unable to find element '{$name}'");
throw new ReactorException(\sprintf("Unable to find element '%s'", $name));
}
}
7 changes: 0 additions & 7 deletions src/Reactor/src/Aggregator/Constants.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Reactor\Aggregator;
Expand Down
7 changes: 0 additions & 7 deletions src/Reactor/src/Aggregator/Methods.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Reactor\Aggregator;
Expand Down
12 changes: 1 addition & 11 deletions src/Reactor/src/Aggregator/Parameters.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Reactor\Aggregator;
Expand Down Expand Up @@ -45,9 +38,6 @@ public function get(string $name): Parameter
return parent::get($name);
}

/**
* {@inheritdoc}
*/
public function render(int $indentLevel = 0): string
{
/**
Expand All @@ -58,6 +48,6 @@ public function render(int $indentLevel = 0): string
$parameters[] = $element->render(0);
}

return implode(', ', $parameters);
return \implode(', ', $parameters);
}
}
7 changes: 0 additions & 7 deletions src/Reactor/src/Aggregator/Properties.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Reactor\Aggregator;
Expand Down
Loading