FrameCollection::count()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * Whoops - php errors for cool kids
4
 * @author Filipe Dobreira <http://github.com/filp>
5
 */
6
7
namespace Whoops\Exception;
8
9
use ArrayAccess;
10
use ArrayIterator;
11
use Countable;
12
use IteratorAggregate;
13
use Serializable;
14
use UnexpectedValueException;
15
16
/**
17
 * Exposes a fluent interface for dealing with an ordered list
18
 * of stack-trace frames.
19
 */
20
class FrameCollection implements ArrayAccess, IteratorAggregate, Serializable, Countable
21
{
22
    /**
23
     * @var array[]
24
     */
25
    private $frames;
26
27
    /**
28
     * @param array $frames
29
     */
30 1
    public function __construct(array $frames)
31
    {
32
        $this->frames = array_map(function ($frame) {
33 1
            return new Frame($frame);
34 1
        }, $frames);
35 1
    }
36
37
    /**
38
     * Filters frames using a callable, returns the same FrameCollection
39
     *
40
     * @param  callable        $callable
41
     * @return FrameCollection
42
     */
43 1
    public function filter($callable)
44
    {
45 1
        $this->frames = array_values(array_filter($this->frames, $callable));
0 ignored issues
show
Documentation Bug introduced by
It seems like array_values(array_filte...is->frames, $callable)) of type array<integer,?> is incompatible with the declared type array<integer,array> of property $frames.

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 1
        return $this;
47
    }
48
49
    /**
50
     * Map the collection of frames
51
     *
52
     * @param  callable        $callable
53
     * @return FrameCollection
54
     */
55 2
    public function map($callable)
56
    {
57
        // Contain the map within a higher-order callable
58
        // that enforces type-correctness for the $callable
59
        $this->frames = array_map(function ($frame) use ($callable) {
60 2
            $frame = call_user_func($callable, $frame);
61
62 2
            if (!$frame instanceof Frame) {
63 1
                throw new UnexpectedValueException(
64 1
                    "Callable to " . __CLASS__ . "::map must return a Frame object"
65 1
                );
66
            }
67
68 1
            return $frame;
69 2
        }, $this->frames);
70
71 1
        return $this;
72
    }
73
74
    /**
75
     * Returns an array with all frames, does not affect
76
     * the internal array.
77
     *
78
     * @todo   If this gets any more complex than this,
79
     *         have getIterator use this method.
80
     * @see    FrameCollection::getIterator
81
     * @return array
82
     */
83 2
    public function getArray()
84
    {
85 2
        return $this->frames;
86
    }
87
88
    /**
89
     * @see IteratorAggregate::getIterator
90
     * @return ArrayIterator
91
     */
92 2
    public function getIterator()
93
    {
94 2
        return new ArrayIterator($this->frames);
95
    }
96
97
    /**
98
     * @see ArrayAccess::offsetExists
99
     * @param int $offset
100
     */
101 1
    public function offsetExists($offset)
102
    {
103 1
        return isset($this->frames[$offset]);
104
    }
105
106
    /**
107
     * @see ArrayAccess::offsetGet
108
     * @param int $offset
109
     */
110 1
    public function offsetGet($offset)
111
    {
112 1
        return $this->frames[$offset];
113
    }
114
115
    /**
116
     * @see ArrayAccess::offsetSet
117
     * @param int $offset
118
     */
119 1
    public function offsetSet($offset, $value)
120
    {
121 1
        throw new \Exception(__CLASS__ . ' is read only');
122
    }
123
124
    /**
125
     * @see ArrayAccess::offsetUnset
126
     * @param int $offset
127
     */
128 1
    public function offsetUnset($offset)
129
    {
130 1
        throw new \Exception(__CLASS__ . ' is read only');
131
    }
132
133
    /**
134
     * @see Countable::count
135
     * @return int
136
     */
137 2
    public function count()
138
    {
139 2
        return count($this->frames);
140
    }
141
142
    /**
143
     * Count the frames that belongs to the application.
144
     *
145
     * @return int
146
     */
147
    public function countIsApplication()
148
    {
149
        return count(array_filter($this->frames, function (Frame $f) {
150
            return $f->isApplication();
151
        }));
152
    }
153
154
    /**
155
     * @see Serializable::serialize
156
     * @return string
157
     */
158 1
    public function serialize()
159
    {
160 1
        return serialize($this->frames);
161
    }
162
163
    /**
164
     * @see Serializable::unserialize
165
     * @param string $serializedFrames
166
     */
167 1
    public function unserialize($serializedFrames)
168
    {
169 1
        $this->frames = unserialize($serializedFrames);
170 1
    }
171
172
    /**
173
     * @param Frame[] $frames Array of Frame instances, usually from $e->getPrevious()
174
     */
175
    public function prependFrames(array $frames)
176
    {
177
        $this->frames = array_merge($frames, $this->frames);
178
    }
179
180
    /**
181
     * Gets the innermost part of stack trace that is not the same as that of outer exception
182
     *
183
     * @param  FrameCollection $parentFrames Outer exception frames to compare tail against
184
     * @return Frame[]
185
     */
186 1
    public function topDiff(FrameCollection $parentFrames)
187
    {
188 1
        $diff = $this->frames;
189
190 1
        $parentFrames = $parentFrames->getArray();
191 1
        $p = count($parentFrames)-1;
192
193 1
        for ($i = count($diff)-1; $i >= 0 && $p >= 0; $i--) {
194
            /** @var Frame $tailFrame */
195 1
            $tailFrame = $diff[$i];
196 1
            if ($tailFrame->equals($parentFrames[$p])) {
0 ignored issues
show
Documentation introduced by
$parentFrames[$p] is of type array, but the function expects a object<Whoops\Exception\Frame>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
197 1
                unset($diff[$i]);
198 1
            }
199 1
            $p--;
200 1
        }
201 1
        return $diff;
202
    }
203
}
204