These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace DusanKasan\Knapsack; |
||
4 | |||
5 | use DusanKasan\Knapsack\Exceptions\InvalidArgument; |
||
6 | use IteratorAggregate; |
||
7 | use RecursiveArrayIterator; |
||
8 | use Traversable; |
||
9 | |||
10 | class Collection implements IteratorAggregate, \Serializable |
||
11 | { |
||
12 | use CollectionTrait; |
||
13 | |||
14 | /** |
||
15 | * @var Traversable |
||
16 | */ |
||
17 | protected $input; |
||
18 | |||
19 | /** |
||
20 | * @var callable |
||
21 | */ |
||
22 | private $inputFactory; |
||
23 | |||
24 | /** |
||
25 | * @param callable|array|Traversable $input If callable is passed, it must return an array|Traversable. |
||
26 | */ |
||
27 | 106 | public function __construct($input) |
|
28 | { |
||
29 | 106 | if (is_callable($input)) { |
|
30 | 81 | $this->inputFactory = $input; |
|
31 | 81 | $input = $input(); |
|
32 | 81 | } |
|
33 | |||
34 | 106 | if (is_array($input)) { |
|
35 | 91 | $this->input = new RecursiveArrayIterator($input); |
|
36 | 106 | } elseif ($input instanceof Traversable) { |
|
37 | 81 | $this->input = $input; |
|
38 | 81 | } else { |
|
39 | 2 | throw new InvalidArgument; |
|
40 | } |
||
41 | 104 | } |
|
42 | |||
43 | /** |
||
44 | * Static alias of normal constructor. |
||
45 | * |
||
46 | * @param callable|array|Traversable $input |
||
47 | * @return Collection |
||
48 | */ |
||
49 | 8 | public static function from($input) |
|
50 | { |
||
51 | 8 | return new self($input); |
|
52 | } |
||
53 | |||
54 | /** |
||
55 | * Returns lazy collection of values, where first value is $input and all subsequent values are computed by applying |
||
56 | * $function to the last value in the collection. By default this produces an infinite collection. However you can |
||
57 | * end the collection by throwing a NoMoreItems exception. |
||
58 | * |
||
59 | * @param mixed $input |
||
60 | * @param callable $function |
||
61 | * @return Collection |
||
62 | */ |
||
63 | 2 | public static function iterate($input, callable $function) |
|
64 | { |
||
65 | 2 | return iterate($input, $function); |
|
66 | } |
||
67 | |||
68 | /** |
||
69 | * Returns a lazy collection of $value repeated $times times. If $times is not provided the collection is infinite. |
||
70 | * |
||
71 | * @param mixed $value |
||
72 | * @param int $times |
||
73 | * @return Collection |
||
74 | */ |
||
75 | 3 | public static function repeat($value, $times = -1) |
|
76 | { |
||
77 | 3 | return repeat($value, $times); |
|
78 | } |
||
79 | |||
80 | /** |
||
81 | * Returns a lazy collection of numbers starting at $start, incremented by $step until $end is reached. |
||
82 | * |
||
83 | * @param int $start |
||
84 | * @param int|null $end |
||
85 | * @param int $step |
||
86 | * @return Collection |
||
87 | */ |
||
88 | 2 | public static function range($start = 0, $end = null, $step = 1) |
|
89 | { |
||
90 | 2 | return \DusanKasan\Knapsack\range($start, $end, $step); |
|
91 | } |
||
92 | |||
93 | /** |
||
94 | * {@inheritdoc} |
||
95 | */ |
||
96 | 103 | public function getIterator() |
|
97 | { |
||
98 | 103 | if ($this->inputFactory) { |
|
99 | 80 | $input = call_user_func($this->inputFactory); |
|
100 | |||
101 | 80 | if (is_array($input)) { |
|
102 | 1 | $input = new RecursiveArrayIterator($input); |
|
103 | 1 | } |
|
104 | |||
105 | 80 | $this->input = $input; |
|
106 | 80 | } |
|
107 | |||
108 | 103 | return $this->input; |
|
0 ignored issues
–
show
|
|||
109 | } |
||
110 | |||
111 | /** |
||
112 | * {@inheritdoc} |
||
113 | */ |
||
114 | 1 | public function serialize() |
|
115 | { |
||
116 | 1 | return serialize( |
|
117 | 1 | toArray( |
|
118 | 1 | map( |
|
119 | 1 | $this->input, |
|
120 | 1 | function ($value, $key) { |
|
121 | 1 | return [$key, $value]; |
|
122 | } |
||
123 | 1 | ) |
|
124 | 1 | ) |
|
125 | 1 | ); |
|
126 | } |
||
127 | |||
128 | /** |
||
129 | * {@inheritdoc} |
||
130 | */ |
||
131 | 1 | public function unserialize($serialized) |
|
132 | { |
||
133 | 1 | $this->input = dereferenceKeyValue(unserialize($serialized)); |
|
134 | 1 | } |
|
135 | } |
||
136 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.