Issues (9)

src/Domain/DonorCollection.php (2 issues)

1
<?php
2
3
/**
4
 * This file is part of byrokrat\giroapp.
5
 *
6
 * byrokrat\giroapp is free software: you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License as published
8
 * by the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * byrokrat\giroapp is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with byrokrat\giroapp. If not, see <http://www.gnu.org/licenses/>.
18
 *
19
 * Copyright 2016-21 Hannes Forsgård
20
 */
21
22
declare(strict_types=1);
23
24
namespace byrokrat\giroapp\Domain;
25
26
use byrokrat\giroapp\Filter\FilterInterface;
27
use byrokrat\giroapp\Sorter\SorterInterface;
28
29
/** @implements \IteratorAggregate<Donor> */
30
class DonorCollection implements \IteratorAggregate
31
{
32
    /**
33
     * @var callable
34
     */
35
    private $factory;
36
37
    /**
38
     * If created with a callable it must itself return a donor iterator
39
     *
40
     * @param iterable<Donor>|callable|mixed $donors
41
     */
42
    public function __construct($donors)
43
    {
44
        if (is_callable($donors)) {
45
            $this->factory = $donors;
0 ignored issues
show
Documentation Bug introduced by
It seems like $donors of type iterable is incompatible with the declared type callable of property $factory.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
46
            return;
47
        }
48
49
        if (is_iterable($donors)) {
50
            $this->factory = function () use ($donors) {
51
                foreach ($donors as $donor) {
52
                    yield $donor;
53
                }
54
            };
55
            return;
56
        }
57
58
        throw new \InvalidArgumentException('Invalid DonorCollection argument');
59
    }
60
61
    /**
62
     * @return iterable<Donor>
63
     */
64
    public function getIterator(): iterable
65
    {
66
        return ($this->factory)();
67
    }
68
69
    public function filter(FilterInterface $filter): DonorCollection
70
    {
71
        return new DonorCollection(function () use ($filter) {
0 ignored issues
show
function(...) { /* ... */ } of type callable is incompatible with the type iterable expected by parameter $donors of byrokrat\giroapp\Domain\...llection::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

71
        return new DonorCollection(/** @scrutinizer ignore-type */ function () use ($filter) {
Loading history...
72
            foreach ($this->getIterator() as $donor) {
73
                if ($filter->filterDonor($donor)) {
74
                    yield $donor;
75
                }
76
            }
77
        });
78
    }
79
80
    public function sort(SorterInterface $sorter): DonorCollection
81
    {
82
        $donors = iterator_to_array($this);
83
        usort($donors, [$sorter, 'compareDonors']);
84
        return new DonorCollection($donors);
85
    }
86
}
87