Skip to content

Commit

Permalink
Merge pull request #94 from samsonasik/apply-php80
Browse files Browse the repository at this point in the history
Apply PHP 8.0 Syntax and constructor promotion
  • Loading branch information
Ocramius authored Oct 15, 2022
2 parents 195c461 + fc76007 commit 6455f15
Show file tree
Hide file tree
Showing 35 changed files with 91 additions and 189 deletions.
2 changes: 1 addition & 1 deletion psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@
<code>$data</code>
</MissingClosureParamType>
<MissingClosureReturnType occurrences="1">
<code>function ($data) {</code>
<code>static function ($data) {</code>
</MissingClosureReturnType>
<MixedArgument occurrences="1">
<code>$data</code>
Expand Down
4 changes: 2 additions & 2 deletions src/AbstractHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public function removeStrategy(string $name): void
* @param null|object $object The object is optionally provided as context.
* @return mixed
*/
public function extractValue(string $name, $value, ?object $object = null)
public function extractValue(string $name, mixed $value, ?object $object = null)
{
return $this->hasStrategy($name)
? $this->getStrategy($name)->extract($value, $object)
Expand All @@ -129,7 +129,7 @@ public function extractValue(string $name, $value, ?object $object = null)
* @param null|array $data The whole data is optionally provided as context.
* @return mixed
*/
public function hydrateValue(string $name, $value, ?array $data = null)
public function hydrateValue(string $name, mixed $value, ?array $data = null)
{
return $this->hasStrategy($name)
? $this->getStrategy($name)->hydrate($value, $data)
Expand Down
8 changes: 2 additions & 6 deletions src/Aggregate/ExtractEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,13 @@ class ExtractEvent extends Event
*/
protected $name = self::EVENT_EXTRACT;

/** @var object */
protected $extractionObject;

/** @var mixed[] Data being extracted from the $extractionObject */
protected $extractedData = [];

public function __construct(object $target, object $extractionObject)
public function __construct(object $target, protected object $extractionObject)
{
parent::__construct();
$this->target = $target;
$this->extractionObject = $extractionObject;
$this->target = $target;
}

/**
Expand Down
15 changes: 2 additions & 13 deletions src/Aggregate/HydrateEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,14 @@ class HydrateEvent extends Event
*/
protected $name = self::EVENT_HYDRATE;

/**
* @var object
* @psalm-var T
*/
protected $hydratedObject;

/** @var mixed[] Data being used to hydrate the $hydratedObject */
protected $hydrationData;

/**
* @param mixed[] $hydrationData Data being used to hydrate the $hydratedObject
* @psalm-param T $hydratedObject
*/
public function __construct(object $target, object $hydratedObject, array $hydrationData)
public function __construct(object $target, protected object $hydratedObject, protected array $hydrationData)
{
parent::__construct();
$this->target = $target;
$this->hydratedObject = $hydratedObject;
$this->hydrationData = $hydrationData;
$this->target = $target;
}

/**
Expand Down
6 changes: 1 addition & 5 deletions src/Aggregate/HydratorListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,8 @@
*/
class HydratorListener extends AbstractListenerAggregate
{
/** @var HydratorInterface */
protected $hydrator;

public function __construct(HydratorInterface $hydrator)
public function __construct(protected HydratorInterface $hydrator)
{
$this->hydrator = $hydrator;
}

/**
Expand Down
7 changes: 4 additions & 3 deletions src/ClassMethodsHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
use function property_exists;
use function spl_object_hash;
use function sprintf;
use function strpos;
use function str_contains;
use function str_starts_with;
use function substr;
use function ucfirst;

Expand Down Expand Up @@ -129,7 +130,7 @@ public function getMethodExistsCheck(): bool
public function extract(object $object): array
{
$objectClass = $object::class;
$isAnonymous = false !== strpos($objectClass, '@anonymous');
$isAnonymous = str_contains($objectClass, '@anonymous');

if ($isAnonymous) {
$objectClass = spl_object_hash($object);
Expand Down Expand Up @@ -206,7 +207,7 @@ private function initCompositeFilter(object $object): Filter\FilterComposite

private function identifyAttributeName(object $object, string $method): string
{
if (strpos($method, 'get') === 0) {
if (str_starts_with($method, 'get')) {
$attribute = substr($method, 3);
return property_exists($object, $attribute) ? $attribute : lcfirst($attribute);
}
Expand Down
6 changes: 1 addition & 5 deletions src/DelegatingHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,8 @@

class DelegatingHydrator implements HydratorInterface
{
/** @var ContainerInterface */
protected $hydrators;

public function __construct(ContainerInterface $hydrators)
public function __construct(protected ContainerInterface $hydrators)
{
$this->hydrators = $hydrators;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Filter/FilterComposite.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ private function executeFilter($filter, string $property, ?object $instance = nu
* @throws InvalidArgumentException If $filter is neither a
* callable nor FilterInterface.
*/
private function validateFilter($filter, string $name): void
private function validateFilter(mixed $filter, string $name): void
{
if (! is_callable($filter) && ! $filter instanceof FilterInterface) {
throw new InvalidArgumentException(sprintf(
Expand Down
24 changes: 10 additions & 14 deletions src/Filter/MethodMatchFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,20 @@

final class MethodMatchFilter implements FilterInterface
{
/**
* The method to exclude
*/
private string $method;

/**
* Either an exclude or an include
*/
private bool $exclude;

/**
* @param string $method The method to exclude or include
* @param bool $exclude If the method should be excluded
*/
public function __construct(string $method, bool $exclude = true)
{
$this->method = $method;
$this->exclude = $exclude;
public function __construct(
/**
* The method to exclude
*/
private string $method,
/**
* Either an exclude or an include
*/
private bool $exclude = true
) {
}

public function filter(string $property, ?object $instance = null): bool
Expand Down
16 changes: 7 additions & 9 deletions src/Filter/NumberOfParameterFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,15 @@

final class NumberOfParameterFilter implements FilterInterface
{
/**
* The number of parameters being accepted
*/
private int $numberOfParameters;

/**
* @param int $numberOfParameters Number of accepted parameters
*/
public function __construct(int $numberOfParameters = 0)
{
$this->numberOfParameters = $numberOfParameters;
public function __construct(
/**
* The number of parameters being accepted
*/
private int $numberOfParameters = 0
) {
}

/**
Expand All @@ -34,7 +32,7 @@ public function filter(string $property, ?object $instance = null): bool
$reflectionMethod = $instance !== null
? new ReflectionMethod($instance, $property)
: new ReflectionMethod($property);
} catch (ReflectionException $exception) {
} catch (ReflectionException) {
throw new InvalidArgumentException(sprintf(
'Method %s does not exist',
$property
Expand Down
2 changes: 1 addition & 1 deletion src/Filter/OptionalParametersFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function filter(string $property, ?object $instance = null): bool
$reflectionMethod = $instance !== null
? new ReflectionMethod($instance, $property)
: new ReflectionMethod($property);
} catch (ReflectionException $exception) {
} catch (ReflectionException) {
throw new InvalidArgumentException(sprintf('Method %s does not exist', $property));
}

Expand Down
2 changes: 1 addition & 1 deletion src/NamingStrategy/MapNamingStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ private function __construct()
*/
private function flipMapping(array $array): array
{
array_walk($array, function ($value, $key) {
array_walk($array, static function ($value, $key): void {
if (! is_string($value) || $value === '') {
throw new Exception\InvalidArgumentException(
'Mapping array can not be flipped because of invalid value'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,11 @@
*/
class PcreReplacement
{
/** @var string */
public $pattern;

/** @var callable */
public $replacement;

public function __construct(string $pattern, callable $replacement)
public function __construct(public string $pattern, callable $replacement)
{
$this->pattern = $pattern;
$this->replacement = $replacement;
}
}
6 changes: 2 additions & 4 deletions src/Strategy/BooleanStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@
*/
final class BooleanStrategy implements StrategyInterface
{
/** @var int|string */
private $trueValue;
private int|string $trueValue;

/** @var int|string */
private $falseValue;
private int|string $falseValue;

/**
* @param int|string $trueValue
Expand Down
8 changes: 2 additions & 6 deletions src/Strategy/CollectionStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,19 @@

class CollectionStrategy implements StrategyInterface
{
private HydratorInterface $objectHydrator;

private string $objectClassName;

/**
* @throws Exception\InvalidArgumentException
*/
public function __construct(HydratorInterface $objectHydrator, string $objectClassName)
public function __construct(private HydratorInterface $objectHydrator, string $objectClassName)
{
if (! class_exists($objectClassName)) {
throw new Exception\InvalidArgumentException(sprintf(
'Object class name needs to be the name of an existing class, got "%s" instead.',
$objectClassName
));
}

$this->objectHydrator = $objectHydrator;
$this->objectClassName = $objectClassName;
}

Expand All @@ -53,7 +49,7 @@ public function extract($value, ?object $object = null)
));
}

return array_map(function ($object) {
return array_map(function ($object): array {
if (! $object instanceof $this->objectClassName) {
throw new Exception\InvalidArgumentException(sprintf(
'Value needs to be an instance of "%s", got "%s" instead.',
Expand Down
28 changes: 9 additions & 19 deletions src/Strategy/DateTimeFormatterStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,6 @@

final class DateTimeFormatterStrategy implements StrategyInterface
{
/**
* Format to use during hydration.
*/
private string $format;

private ?DateTimeZone $timezone;

/**
* Format to use during extraction.
*
Expand All @@ -33,24 +26,21 @@ final class DateTimeFormatterStrategy implements StrategyInterface
*/
private string $extractionFormat;

/**
* Whether or not to allow hydration of values that do not follow the format exactly.
*/
private bool $dateTimeFallback;

/**
* @param bool $dateTimeFallback try to parse with DateTime when createFromFormat fails
* @throws Exception\InvalidArgumentException For invalid $format values.
*/
public function __construct(
string $format = DateTime::RFC3339,
?DateTimeZone $timezone = null,
bool $dateTimeFallback = false
/**
* Format to use during hydration.
*/
private string $format = DateTime::RFC3339,
private ?DateTimeZone $timezone = null,
/**
* Whether or not to allow hydration of values that do not follow the format exactly.
*/
private bool $dateTimeFallback = false
) {
$this->format = $format;
$this->timezone = $timezone;
$this->dateTimeFallback = $dateTimeFallback;

$extractionFormat = preg_replace('/(?<![\\\\])[+|!\*]/', '', $this->format);
if (null === $extractionFormat) {
throw new Exception\InvalidArgumentException(sprintf(
Expand Down
5 changes: 1 addition & 4 deletions src/Strategy/DateTimeImmutableFormatterStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,8 @@

class DateTimeImmutableFormatterStrategy implements StrategyInterface
{
private DateTimeFormatterStrategy $dateTimeStrategy;

public function __construct(DateTimeFormatterStrategy $dateTimeStrategy)
public function __construct(private DateTimeFormatterStrategy $dateTimeStrategy)
{
$this->dateTimeStrategy = $dateTimeStrategy;
}

/**
Expand Down
5 changes: 1 addition & 4 deletions src/Strategy/ExplodeStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,15 @@ final class ExplodeStrategy implements StrategyInterface
{
private string $valueDelimiter;

private ?int $explodeLimit;

/**
* Constructor
*
* @param string $delimiter String that the values will be split upon
* @param int|null $explodeLimit Explode limit
*/
public function __construct(string $delimiter = ',', ?int $explodeLimit = null)
public function __construct(string $delimiter = ',', private ?int $explodeLimit = null)
{
$this->setValueDelimiter($delimiter);
$this->explodeLimit = $explodeLimit;
}

/**
Expand Down
6 changes: 1 addition & 5 deletions src/Strategy/HydratorStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@

class HydratorStrategy implements StrategyInterface
{
private HydratorInterface $objectHydrator;

private string $objectClassName;

/**
* @throws Exception\InvalidArgumentException
*/
public function __construct(
HydratorInterface $objectHydrator,
private HydratorInterface $objectHydrator,
string $objectClassName
) {
if (! class_exists($objectClassName)) {
Expand All @@ -35,8 +33,6 @@ public function __construct(
)
);
}

$this->objectHydrator = $objectHydrator;
$this->objectClassName = $objectClassName;
}

Expand Down
Loading

0 comments on commit 6455f15

Please sign in to comment.