Skip to content

Commit

Permalink
chore: Improve typing information (#61)
Browse files Browse the repository at this point in the history
Main points of attention: `ImmutableCollectionInterface`;
- `@implements \IteratorAggregate<TKey, T>` -> `@extends \IteratorAggregate<TKey, T>`
- `@implements \ArrayAccess<TKey, T>` -> `@extends \ArrayAccess<TKey, T>`

This change should help to resolve type information when iterating over
collections.
  • Loading branch information
frankverhoeven authored Feb 21, 2023
1 parent 432cabf commit d531579
Show file tree
Hide file tree
Showing 46 changed files with 199 additions and 332 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ vendor
composer.lock
phpunit.xml
.phpcs-cache
.phpunit.cache
.phpunit.result.cache
rector.php
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@
"doctrine/orm": "^2.13",
"myonlinestore/coding-standard": "^4.0",
"phpbench/phpbench": "^1.2",
"phpunit/phpunit": "^9.5",
"psalm/plugin-phpunit": "^0.17",
"phpunit/phpunit": "^10.0",
"psalm/plugin-phpunit": "^0.18",
"symfony/phpunit-bridge": "^6.1",
"vimeo/psalm": "^4.29"
"vimeo/psalm": "^5.6"
}
}
17 changes: 2 additions & 15 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

<phpunit bootstrap="vendor/autoload.php"
colors="true"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
cacheDirectory=".phpunit.cache"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
>
<coverage includeUncoveredFiles="false">
<include>
Expand All @@ -14,20 +15,6 @@
</exclude>
</coverage>

<listeners>
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener">
<arguments>
<array>
<!-- set this option to 0 to disable the DebugClassLoader integration -->
<!-- this will remove the 'class X might add return type Y' messages -->
<element key="debug-class-loader">
<integer>0</integer>
</element>
</array>
</arguments>
</listener>
</listeners>

<testsuites>
<testsuite name="Test Suite">
<directory>./tests/</directory>
Expand Down
106 changes: 16 additions & 90 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,116 +1,60 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="4.24.0@06dd975cb55d36af80f242561738f16c5f58264f">
<file src="src/Assertion/EnumValueGuardTrait.php">
<InvalidScalarArgument occurrences="1">
<code>$value</code>
</InvalidScalarArgument>
</file>
<file src="src/Collection/ImmutableCollection.php">
<DeprecatedClass occurrences="1">
<code>MutableCollection</code>
</DeprecatedClass>
<DeprecatedInterface occurrences="1">
<code>ImmutableCollection</code>
</DeprecatedInterface>
</file>
<files psalm-version="5.6.0@e784128902dfe01d489c4123d69918a9f3c1eac5">
<file src="src/Collection/ImmutableCollectionInterface.php">
<MissingReturnType occurrences="1">
<MissingReturnType>
<code>each</code>
</MissingReturnType>
</file>
<file src="src/Collection/MutableCollection.php">
<DeprecatedClass occurrences="1">
<code>ImmutableCollectionInterface</code>
</DeprecatedClass>
<DeprecatedInterface occurrences="1">
<code>MutableCollection</code>
</DeprecatedInterface>
<MissingReturnType occurrences="1">
<MissingReturnType>
<code>each</code>
</MissingReturnType>
<UnsafeInstantiation occurrences="3">
<UnsafeInstantiation>
<code>new static(\array_filter($this-&gt;toArray(), $closure))</code>
<code>new static(\array_map($closure, $this-&gt;toArray()))</code>
<code>new static(\array_values($this-&gt;toArray()))</code>
</UnsafeInstantiation>
</file>
<file src="src/Collection/RegionCodeCollection.php">
<DeprecatedClass occurrences="4">
<code>ImmutableCollection</code>
<code>RegionCodeCollectionInterface</code>
<code>RegionCodeCollectionInterface</code>
</DeprecatedClass>
<DeprecatedInterface occurrences="1">
<code>RegionCodeCollection</code>
</DeprecatedInterface>
<DeprecatedTrait occurrences="1">
<code>StringCollectionTrait</code>
</DeprecatedTrait>
</file>
<file src="src/Collection/RegionCodeCollectionInterface.php">
<LessSpecificImplementedReturnType occurrences="1">
<LessSpecificImplementedReturnType>
<code>RegionCodeCollectionInterface</code>
</LessSpecificImplementedReturnType>
</file>
<file src="src/Collection/StoreIds.php">
<ArgumentTypeCoercion occurrences="1">
<code>$this-&gt;toArray()</code>
</ArgumentTypeCoercion>
<DeprecatedClass occurrences="7">
<code>ImmutableCollection</code>
<code>StoreId</code>
<code>StoreId[]|int[]</code>
<code>StoreIds</code>
<code>StoreIdsInterface</code>
<code>new StoreId($entry)</code>
</DeprecatedClass>
<DeprecatedInterface occurrences="1">
<code>StoreIds</code>
</DeprecatedInterface>
<DeprecatedTrait occurrences="1">
<code>StringCollectionTrait</code>
</DeprecatedTrait>
</file>
<file src="src/Collection/StoreIdsInterface.php">
<DeprecatedClass occurrences="1">
<code>StoreId</code>
</DeprecatedClass>
</file>
<file src="src/Value/Arithmetic/Amount.php">
<MissingParamType occurrences="1">
<MissingParamType>
<code>$value</code>
</MissingParamType>
</file>
<file src="src/Value/Contact/PhoneNumber.php">
<PossiblyNullArgument occurrences="1">
<PossiblyNullArgument>
<code>$this-&gt;value-&gt;getNationalNumber()</code>
</PossiblyNullArgument>
</file>
<file src="src/Value/Monetary/Amount.php">
<MissingParamType occurrences="1">
<MissingParamType>
<code>$value</code>
</MissingParamType>
</file>
<file src="src/Value/Money/Money.php">
<ArgumentTypeCoercion occurrences="3">
<ArgumentTypeCoercion>
<code>(string) $this-&gt;amount</code>
<code>\sprintf('1%s', \str_repeat('0', $currency-&gt;getMinorUnit()))</code>
<code>\sprintf('1%s', \str_repeat('0', $this-&gt;currency-&gt;getMinorUnit()))</code>
</ArgumentTypeCoercion>
</file>
<file src="src/Value/Money/Price.php">
<ArgumentTypeCoercion occurrences="2">
<ArgumentTypeCoercion>
<code>(string) $amount</code>
<code>(string) $amount</code>
</ArgumentTypeCoercion>
</file>
<file src="src/Value/RegionCode.php">
<DocblockTypeContradiction occurrences="1">
<DocblockTypeContradiction>
<code>null === $code</code>
</DocblockTypeContradiction>
</file>
<file src="src/Value/Web/DomainName.php">
<ImpureMethodCall occurrences="16">
<ImpureMethodCall>
<code>fromString</code>
<code>registrableDomain</code>
<code>registrableDomain</code>
Expand All @@ -128,19 +72,19 @@
<code>value</code>
<code>value</code>
</ImpureMethodCall>
<ImpureStaticProperty occurrences="3">
<ImpureStaticProperty>
<code>self::$rules</code>
<code>self::$rules</code>
<code>self::$rules</code>
</ImpureStaticProperty>
</file>
<file src="src/Value/Web/IPAddress.php">
<RedundantCastGivenDocblockType occurrences="1">
<RedundantCastGivenDocblockType>
<code>(string) $this-&gt;value</code>
</RedundantCastGivenDocblockType>
</file>
<file src="src/Value/Web/Url.php">
<ImpureMethodCall occurrences="28">
<ImpureMethodCall>
<code>__toString</code>
<code>getAuthority</code>
<code>getFragment</code>
Expand Down Expand Up @@ -170,27 +114,9 @@
<code>withScheme</code>
<code>withUserInfo</code>
</ImpureMethodCall>
<LessSpecificReturnStatement occurrences="7">
<code>new self($this-&gt;leagueUri-&gt;withFragment($fragment))</code>
<code>new self($this-&gt;leagueUri-&gt;withHost($host))</code>
<code>new self($this-&gt;leagueUri-&gt;withPath($path))</code>
<code>new self($this-&gt;leagueUri-&gt;withPort($port))</code>
<code>new self($this-&gt;leagueUri-&gt;withQuery($query))</code>
<code>new self($this-&gt;leagueUri-&gt;withScheme($scheme))</code>
<code>new self($this-&gt;leagueUri-&gt;withUserInfo($user, $password))</code>
</LessSpecificReturnStatement>
<MoreSpecificReturnType occurrences="7">
<code>self</code>
<code>self</code>
<code>self</code>
<code>self</code>
<code>self</code>
<code>self</code>
<code>self</code>
</MoreSpecificReturnType>
</file>
<file src="src/Value/Web/UrlPath.php">
<DocblockTypeContradiction occurrences="1">
<DocblockTypeContradiction>
<code>\is_string($value)</code>
</DocblockTypeContradiction>
</file>
Expand Down
2 changes: 2 additions & 0 deletions psalm.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

<psalm errorBaseline="psalm-baseline.xml"
errorLevel="2"
findUnusedBaselineEntry="true"
findUnusedCode="false"
resolveFromConfigFile="true"
usePhpDocMethodsWithoutMagicCall="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
Expand Down
1 change: 1 addition & 0 deletions src/Assertion/EnumValueGuardTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function guardIsValidValue($value)
$validValues = $this->getValidValues();

if (!\in_array($value, $validValues, true)) {
/** @psalm-suppress ArgumentTypeCoercion */
throw new \InvalidArgumentException(
\sprintf(
'Invalid %s value given: "%s" (valid values: %s)',
Expand Down
4 changes: 2 additions & 2 deletions src/Collection/ImmutableCollectionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
/**
* @template TKey of array-key
* @template T
* @implements \IteratorAggregate<TKey, T>
* @implements \ArrayAccess<TKey, T>
* @extends \IteratorAggregate<TKey, T>
* @extends \ArrayAccess<TKey, T>
*/
interface ImmutableCollectionInterface extends \ArrayAccess, \Countable, \IteratorAggregate
{
Expand Down
6 changes: 2 additions & 4 deletions src/Collection/RegionCodeCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@

use MyOnlineStore\Common\Domain\Value\RegionCode;

/**
* @extends ImmutableCollection<array-key, RegionCode>
* @use StringCollectionTrait<array-key, RegionCode>
*/
/** @extends ImmutableCollection<array-key, RegionCode> */
final class RegionCodeCollection extends ImmutableCollection implements RegionCodeCollectionInterface
{
/** @use StringCollectionTrait<array-key, RegionCode> */
use StringCollectionTrait;

/** @inheritdoc */
Expand Down
3 changes: 1 addition & 2 deletions src/Collection/RegionCodeCollectionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@

use MyOnlineStore\Common\Domain\Value\RegionCode;

/** @method RegionCode[] getIterator() */
/** @extends ImmutableCollectionInterface<array-key, RegionCode> */
interface RegionCodeCollectionInterface extends ImmutableCollectionInterface, StringCollectionInterface
{
/** @return RegionCodeCollectionInterface */
public function reindex();

/** @return RegionCodeCollectionInterface */
public function unique(): RegionCodeCollectionInterface;
}
6 changes: 2 additions & 4 deletions src/Collection/StoreIds.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@

use MyOnlineStore\Common\Domain\Value\StoreId;

/**
* @extends ImmutableCollection<array-key, StoreId>
* @use StringCollectionTrait<array-key, StoreId>
*/
/** @extends ImmutableCollection<array-key, StoreId> */
final class StoreIds extends ImmutableCollection implements StoreIdsInterface
{
/** @use StringCollectionTrait<array-key, StoreId> */
use StringCollectionTrait;

/** @param StoreId[]|int[] $entries */
Expand Down
2 changes: 1 addition & 1 deletion src/Collection/StoreIdsInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use MyOnlineStore\Common\Domain\Value\StoreId;

/** @method StoreId[] getIterator() */
/** @extends ImmutableCollectionInterface<array-key, StoreId> */
interface StoreIdsInterface extends ImmutableCollectionInterface
{
/** @throws \InvalidArgumentException */
Expand Down
2 changes: 1 addition & 1 deletion src/Exception/Color/InvalidHexColor.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
final class InvalidHexColor extends InvalidArgument
{
/** @psalm-pure */
public static function withHexColor(string $color, \Throwable|null $previous = null): self
public static function withHexColor(string $color, \Throwable | null $previous = null): self
{
return new self(
\sprintf(
Expand Down
Loading

0 comments on commit d531579

Please sign in to comment.