Issues (25)

src/Events/RetrievePaginatedResourcesEvent.php (1 issue)

1
<?php
2
3
namespace W2w\Lib\Apie\Events;
4
5
use Pagerfanta\Pagerfanta;
6
use Psr\Http\Message\RequestInterface;
7
use Psr\Http\Message\ServerRequestInterface;
8
use W2w\Lib\Apie\Core\SearchFilters\SearchFilterRequest;
9
use W2w\Lib\Apie\Exceptions\InvalidReturnTypeOfApiResourceException;
10
11
/**
12
 * Event mediator for retrieving a list of resources.
13
 */
14
class RetrievePaginatedResourcesEvent
15
{
16
    /**
17
     * @var string
18
     */
19
    private $resourceClass;
20
21
    /**
22
     * @var SearchFilterRequest
23
     */
24
    private $searchFilterRequest;
25
26
    /**
27
     * @var object[]|null
28
     */
29
    private $resources;
30
31
    /**
32
     * @var RequestInterface|null
33
     */
34
    private $request;
35
36
    /**
37
     * @param string $resourceClass
38
     * @param SearchFilterRequest $searchFilterRequest
39
     * @param ServerRequestInterface|null $request
40
     */
41
    public function __construct(string $resourceClass, SearchFilterRequest $searchFilterRequest, ?ServerRequestInterface $request)
42
    {
43
        $this->resourceClass = $resourceClass;
44
        $this->searchFilterRequest = $searchFilterRequest;
45
        $this->request = $request;
46
    }
47
48
    /**
49
     * @return string
50
     */
51
    public function getResourceClass(): string
52
    {
53
        return $this->resourceClass;
54
    }
55
56
    /**
57
     * @return SearchFilterRequest
58
     */
59
    public function getSearchFilterRequest(): SearchFilterRequest
60
    {
61
        return $this->searchFilterRequest;
62
    }
63
64
    /**
65
     * @param object[] $resources
66
     */
67
    public function setResources(iterable $resources)
68
    {
69
        $resourceArray = [];
70
        foreach ($resources as $resource) {
71
            if (!$resource instanceof $this->resourceClass) {
72
                throw new InvalidReturnTypeOfApiResourceException(null, get_class($resource), $this->resourceClass);
73
            }
74
            $resourceArray[] = $resource;
75
        }
76
        $this->resources = is_object($resources) ? $resources : $resourceArray;
0 ignored issues
show
Documentation Bug introduced by
It seems like is_object($resources) ? ...ources : $resourceArray can also be of type object. However, the property $resources is declared as type array<mixed,object>|null. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
77
    }
78
79
    /**
80
     * @return object[]|Pagerfanta|null
81
     */
82
    public function getResources()
83
    {
84
        return $this->resources;
85
    }
86
87
    /**
88
     * @return RequestInterface|null
89
     */
90
    public function getRequest(): ?RequestInterface
91
    {
92
        return $this->request;
93
    }
94
}
95