File: EventInterfaceChecks.php

package info (click to toggle)
php-zend-eventmanager 3.14.0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 904 kB
  • sloc: php: 3,542; xml: 696; makefile: 17
file content (85 lines) | stat: -rw-r--r-- 2,339 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php

declare(strict_types=1);

namespace LaminasPsalm\EventManager;

use Laminas\EventManager\EventInterface;
use LaminasPsalm\EventManager\Model\CheckObject;

class EventInterfaceChecks
{
    /**
     * @param EventInterface<CheckObject, array{foo: int, bar: CheckObject}> $e
     * @return array{
     *     CheckObject,
     *     array{foo: int, bar: CheckObject},
     *     int,
     *     CheckObject
     * }
     */
    public function checkTargetAndParamsMatchTemplate(EventInterface $e): array
    {
        return [
            $e->getTarget(),
            $e->getParams(),
            $e->getParams()['foo'],
            $e->getParams()['bar'],
        ];
    }

    /**
     * @param EventInterface<null, array<empty, empty>> $e
     * @return EventInterface<CheckObject, array<empty, empty>>
     */
    public function checkSetTargetChangesTemplate(EventInterface $e): EventInterface
    {
        $e->setTarget(new CheckObject());
        return $e;
    }

    /**
     * @param EventInterface<null, array{foo: int}> $e
     * @return EventInterface<null, array{foo: CheckObject, bar: 'baz'}>
     */
    public function checkSetParamsChangesTemplate(EventInterface $e): EventInterface
    {
        $e->setParams([
            'foo' => new CheckObject(),
            'bar' => 'baz',
        ]);
        return $e;
    }

    /**
     * Individual params obtained via `getParam()` can't be inferred because their keys/values can't be selected from
     * the template type.
     *
     * @param EventInterface<null, array{foo: int, bar: CheckObject}> $e
     * @return array{
     *     mixed,
     *     mixed
     * }
     */
    public function checkIndividualParamsNotInferred(EventInterface $e): array
    {
        return [
            $e->getParam('foo'),
            $e->getParam('bar'),
        ];
    }

    /**
     * Changing the template and statically checking individual values is not possible with Psalm because
     * key-of and value-of do not work on objects.
     *
     * @param EventInterface<null, array{foo: int}> $e
     * @return EventInterface<null, array{foo: int}>
     */
    public function checkIndividualParamDoesNotChangeTemplate(EventInterface $e): EventInterface
    {
        $e->setParam('foo', 'notAnInt');
        $e->setParam('bar', 'keyDidNotExist');
        return $e;
    }
}