Skip to content

Commit 00663c0

Browse files
authored
HP-2508 Working on typing for Psalm (#101)
* HP-2508 Working on typing for Psalm * Fix Psalm where possible, suppress else * Remove unused suppress statements * Fix PHPUnit test case
1 parent 5343fbe commit 00663c0

17 files changed

+241
-60
lines changed

src/product/BillingRegistryInterface.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ interface BillingRegistryInterface extends HasLockInterface
1313
*/
1414
public function priceTypes(): Generator;
1515

16+
/**
17+
* @return TariffTypeDefinitionInterface[]
18+
*/
1619
public function getTariffTypeDefinitions(): array;
1720

1821
public function addTariffType(TariffTypeDefinitionInterface $tariffTypeDefinition): void;

src/product/TariffTypeDefinition.php

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,22 @@
22

33
namespace hiqdev\php\billing\product;
44

5+
use Google\Service\TPU;
6+
use hiqdev\php\billing\product\behavior\BehaviorCollectionInterface;
57
use hiqdev\php\billing\product\behavior\BehaviorTariffTypeCollection;
68
use hiqdev\php\billing\product\behavior\TariffTypeBehaviorRegistry;
79
use hiqdev\php\billing\product\Domain\Model\TariffTypeInterface;
810
use hiqdev\php\billing\product\Exception\ProductNotDefinedException;
11+
use hiqdev\php\billing\product\price\PriceTypeDefinition;
912
use hiqdev\php\billing\product\price\PriceTypeDefinitionCollection;
13+
use hiqdev\php\billing\product\price\PriceTypeDefinitionCollectionInterface;
1014
use hiqdev\php\billing\product\price\PriceTypeDefinitionFactory;
1115
use hiqdev\php\billing\product\trait\HasLock;
1216

1317
/**
14-
* @implements TariffTypeDefinitionInterface<PriceTypeDefinitionCollection>
18+
* @template TPriceTypeDefinitionCollection of PriceTypeDefinitionCollection
19+
* @implements TariffTypeDefinitionInterface<TPriceTypeDefinitionCollection>
20+
* @psalm-suppress InvalidTemplateParam
1521
*/
1622
class TariffTypeDefinition implements TariffTypeDefinitionInterface
1723
{
@@ -39,7 +45,7 @@ public function belongToTariffType(TariffTypeInterface $tariffType): bool
3945
return $this->tariffType->equals($tariffType);
4046
}
4147

42-
public function ofProduct(ProductInterface $product): TariffTypeDefinitionInterface
48+
public function ofProduct(ProductInterface $product): static
4349
{
4450
$this->ensureNotLocked();
4551
$this->product = $product;
@@ -51,6 +57,7 @@ public function getProduct(): ProductInterface
5157
{
5258
$this->ensureProductExists();
5359

60+
/** @psalm-suppress NullableReturnStatement */
5461
return $this->product;
5562
}
5663

@@ -61,22 +68,32 @@ private function ensureProductExists(): void
6168
}
6269
}
6370

64-
public function setPricesSuggester(string $suggesterClass): TariffTypeDefinitionInterface
71+
public function setPricesSuggester(string $suggesterClass): static
6572
{
6673
$this->ensureNotLocked();
6774

6875
// Validate or store the suggester class
6976
return $this;
7077
}
7178

72-
public function withPrices(): PriceTypeDefinitionCollection
79+
/**
80+
* @psalm-suppress InvalidReturnType
81+
* @psalm-suppress InvalidReturnStatement
82+
*/
83+
public function withPrices()
7384
{
7485
$this->ensureNotLocked();
7586

7687
return $this->prices;
7788
}
7889

79-
public function withBehaviors(): BehaviorTariffTypeCollection
90+
/**
91+
* @return BehaviorCollectionInterface<TariffTypeDefinition>
92+
* @psalm-suppress ImplementedReturnTypeMismatch
93+
* @psalm-suppress InvalidReturnType
94+
* @psalm-suppress InvalidReturnStatement
95+
*/
96+
public function withBehaviors()
8097
{
8198
$this->ensureNotLocked();
8299

src/product/TariffTypeDefinitionInterface.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
use hiqdev\php\billing\product\trait\HasLockInterface;
99

1010
/**
11-
* @template T of PriceTypeDefinitionCollectionInterface
11+
* @template-covariant TPriceTypeDefinitionCollection of PriceTypeDefinitionCollectionInterface
12+
* @extends HasBehaviorsInterface<static>
13+
* @psalm-consistent-templates
14+
* @psalm-suppress InvalidTemplateParam
1215
*/
1316
interface TariffTypeDefinitionInterface extends HasBehaviorsInterface, HasLockInterface
1417
{
@@ -22,17 +25,16 @@ public function tariffType(): TariffTypeInterface;
2225
*/
2326
public function belongToTariffType(TariffTypeInterface $tariffType): bool;
2427

25-
public function ofProduct(ProductInterface $product): self;
28+
public function ofProduct(ProductInterface $product): static;
2629

2730
public function getProduct(): ProductInterface;
2831

29-
public function setPricesSuggester(string $suggesterClass): self;
32+
public function setPricesSuggester(string $suggesterClass): static;
3033

3134
/**
32-
* @return PriceTypeDefinitionCollectionInterface
33-
* @psalm-return T
35+
* @return TPriceTypeDefinitionCollection
3436
*/
35-
public function withPrices(): PriceTypeDefinitionCollectionInterface;
37+
public function withPrices();
3638

37-
public function end(): self;
39+
public function end();
3840
}

src/product/behavior/BehaviorCollection.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,15 @@
33
namespace hiqdev\php\billing\product\behavior;
44

55
use hiqdev\php\billing\product\Domain\Model\TariffTypeInterface;
6+
use hiqdev\php\billing\product\price\PriceTypeDefinitionInterface;
7+
use hiqdev\php\billing\product\TariffTypeDefinitionInterface;
68
use hiqdev\php\billing\product\trait\HasLock;
79

10+
/**
11+
* @template-covariant TParentContext of TariffTypeDefinitionInterface|PriceTypeDefinitionInterface
12+
* @implements BehaviorCollectionInterface<TParentContext>
13+
* @psalm-consistent-templates
14+
*/
815
abstract class BehaviorCollection implements BehaviorCollectionInterface
916
{
1017
use HasLock;
@@ -21,7 +28,7 @@ public function getIterator(): \Traversable
2128
return new \ArrayIterator($this->behaviors);
2229
}
2330

24-
public function attach(BehaviorInterface $behavior): self
31+
public function attach(BehaviorInterface $behavior): static
2532
{
2633
$this->ensureNotLocked();
2734

src/product/behavior/BehaviorCollectionInterface.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
use Traversable;
1010

1111
/**
12+
* @template-covariant TParentContext of TariffTypeDefinitionInterface|PriceTypeDefinitionInterface
1213
* @extends IteratorAggregate<int, BehaviorInterface>
14+
* @psalm-consistent-templates
1315
*/
1416
interface BehaviorCollectionInterface extends IteratorAggregate, HasLockInterface
1517
{
@@ -18,10 +20,10 @@ interface BehaviorCollectionInterface extends IteratorAggregate, HasLockInterfac
1820
*/
1921
public function getIterator(): Traversable;
2022

21-
public function attach(BehaviorInterface $behavior): self;
23+
public function attach(BehaviorInterface $behavior): static;
2224

2325
/**
24-
* @return TariffTypeDefinitionInterface|PriceTypeDefinitionInterface
26+
* @return TParentContext
2527
*/
26-
public function end(): TariffTypeDefinitionInterface|PriceTypeDefinitionInterface;
28+
public function end();
2729
}

src/product/behavior/BehaviorPriceTypeDefinitionCollection.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,35 @@
55
use hiqdev\php\billing\product\Domain\Model\TariffTypeInterface;
66
use hiqdev\php\billing\product\price\PriceTypeDefinitionInterface;
77

8+
/**
9+
* @template TPriceDefinition
10+
* @extends BehaviorCollection<TPriceDefinition>
11+
* @psalm-consistent-templates
12+
* @psalm-suppress InvalidTemplateParam
13+
*/
814
class BehaviorPriceTypeDefinitionCollection extends BehaviorCollection
915
{
10-
public function __construct(private readonly PriceTypeDefinitionInterface $parent, TariffTypeInterface $tariffType)
11-
{
16+
/**
17+
* @psalm-var TPriceDefinition
18+
*/
19+
private readonly PriceTypeDefinitionInterface $parent;
20+
21+
/**
22+
* @psalm-param TPriceDefinition $parent
23+
*/
24+
public function __construct(
25+
PriceTypeDefinitionInterface $parent,
26+
TariffTypeInterface $tariffType
27+
) {
28+
$this->parent = $parent;
29+
1230
parent::__construct($tariffType);
1331
}
1432

15-
public function end(): PriceTypeDefinitionInterface
33+
/**
34+
* @return TPriceDefinition
35+
*/
36+
public function end()
1637
{
1738
return $this->parent;
1839
}

src/product/behavior/BehaviorTariffTypeCollection.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,30 @@
55
use hiqdev\php\billing\product\Domain\Model\TariffTypeInterface;
66
use hiqdev\php\billing\product\TariffTypeDefinitionInterface;
77

8+
/**
9+
* @template T
10+
* @psalm-suppress MissingTemplateParam
11+
* @psalm-suppress InvalidTemplateParam
12+
*/
813
class BehaviorTariffTypeCollection extends BehaviorCollection
914
{
10-
public function __construct(private readonly TariffTypeDefinitionInterface $parent, TariffTypeInterface $tariffType)
11-
{
15+
/**
16+
* @psalm-param T $parent
17+
*/
18+
public function __construct(
19+
/**
20+
* @var T
21+
*/
22+
private readonly TariffTypeDefinitionInterface $parent,
23+
TariffTypeInterface $tariffType
24+
) {
1225
parent::__construct($tariffType);
1326
}
1427

28+
/**
29+
* @psalm-return T
30+
* @psalm-suppress LessSpecificImplementedReturnType
31+
*/
1532
public function end(): TariffTypeDefinitionInterface
1633
{
1734
return $this->parent;

src/product/behavior/HasBehaviorsInterface.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,20 @@
22

33
namespace hiqdev\php\billing\product\behavior;
44

5+
use hiqdev\php\billing\product\invoice\RepresentationCollection;
6+
use hiqdev\php\billing\product\price\PriceTypeDefinitionInterface;
7+
use hiqdev\php\billing\product\TariffTypeDefinitionInterface;
8+
9+
/**
10+
* @template TParentCollection of PriceTypeDefinitionInterface|TariffTypeDefinitionInterface
11+
* @psalm-consistent-templates
12+
*/
513
interface HasBehaviorsInterface
614
{
7-
public function withBehaviors(): BehaviorCollectionInterface;
15+
/**
16+
* @return BehaviorCollectionInterface<TParentCollection>
17+
*/
18+
public function withBehaviors();
819

920
public function hasBehavior(string $behaviorClassName): bool;
1021
}

src/product/behavior/TariffTypeBehaviorRegistry.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,27 @@
2323
* - To avoid code duplication of behavior-related methods in multiple classes.
2424
* - To separate concerns by handling behavior-related logic in a dedicated class.
2525
* - To improve maintainability and testability of tariff behavior handling.
26+
*
27+
* @template-covariant T of TariffTypeDefinitionInterface
2628
*/
2729
final class TariffTypeBehaviorRegistry implements HasLockInterface
2830
{
31+
/**
32+
* @var BehaviorTariffTypeCollection<T>
33+
*/
2934
private BehaviorTariffTypeCollection $behaviorCollection;
3035

36+
/**
37+
* @psalm-param T $tariffTypeDefinition
38+
*/
3139
public function __construct(TariffTypeDefinitionInterface $tariffTypeDefinition, TariffTypeInterface $tariffType)
3240
{
3341
$this->behaviorCollection = new BehaviorTariffTypeCollection($tariffTypeDefinition, $tariffType);
3442
}
3543

44+
/**
45+
* @return BehaviorTariffTypeCollection<T>
46+
*/
3647
public function getBehaviors(): BehaviorTariffTypeCollection
3748
{
3849
return $this->behaviorCollection;

src/product/invoice/RepresentationCollection.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
use Traversable;
1010

1111
/**
12-
* @template T of PriceTypeDefinition
12+
* @template T
1313
* @implements IteratorAggregate<int, RepresentationInterface>
14+
* @psalm-consistent-templates
1415
*/
1516
class RepresentationCollection implements IteratorAggregate, HasLockInterface
1617
{
@@ -20,8 +21,18 @@ class RepresentationCollection implements IteratorAggregate, HasLockInterface
2021

2122
private RepresentationUniquenessGuard $uniquenessGuard;
2223

23-
public function __construct(private readonly PriceTypeDefinition $priceTypeDefinition)
24-
{
24+
/**
25+
* @psalm-var T
26+
*/
27+
private readonly PriceTypeDefinition $priceTypeDefinition;
28+
29+
/**
30+
* @psalm-param T $priceTypeDefinition
31+
*/
32+
public function __construct(
33+
PriceTypeDefinition $priceTypeDefinition,
34+
) {
35+
$this->priceTypeDefinition = $priceTypeDefinition;
2536
$this->uniquenessGuard = new RepresentationUniquenessGuard();
2637
}
2738

@@ -33,7 +44,7 @@ public function getIterator(): Traversable
3344
return new \ArrayIterator($this->representations);
3445
}
3546

36-
public function attach(RepresentationInterface $representation): self
47+
public function attach(RepresentationInterface $representation): static
3748
{
3849
$this->ensureNotLocked();
3950

@@ -49,7 +60,7 @@ public function attach(RepresentationInterface $representation): self
4960
/**
5061
* @psalm-return T
5162
*/
52-
public function end(): PriceTypeDefinition
63+
public function end()
5364
{
5465
return $this->priceTypeDefinition;
5566
}

0 commit comments

Comments
 (0)