Completed
Push — master ( 1ac1c6...30d48c )
by Zoltán
03:04
created

AggregateException   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 47
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 3 1
A hasAny() 0 3 1
A getIterator() 0 3 1
A toArray() 0 3 1
A count() 0 3 1
1
<?php namespace BuildR\Foundation\Exception;
2
3
use BuildR\Foundation\Object\ArrayConvertibleInterface;
4
use \Exception;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, BuildR\Foundation\Exception\Exception.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
5
use \Throwable;
6
use \IteratorAggregate;
7
use \ArrayIterator;
8
use \Countable;
9
10
/**
11
 * Aggregates multiple PHP exception into one exception. Useful when iterating over
12
 * large set of objects and call a method on each that might be raise an exception.
13
 *
14
 * Simply aggregate all thrown exception to this class and throw it when the
15
 * iteration is end.
16
 *
17
 * BuildR PHP Framework
18
 *
19
 * @author Zoltán Borsos <[email protected]>
20
 * @package Foundation
21
 * @subpackage Exception
22
 *
23
 * @copyright    Copyright 2015, Zoltán Borsos.
24
 * @license      https://github.com/Zolli/BuildR/blob/master/LICENSE.md
25
 * @link         https://github.com/Zolli/BuildR
26
 *
27
 * @codeCoverageIgnore
28
 */
29
class AggregateException extends Exception implements \IteratorAggregate, Countable, ArrayConvertibleInterface {
30
31
    /**
32
     * @var array
33
     */
34
    private $collection = [];
35
36
    /**
37
     * Add a new exception to the stack
38
     *
39
     * @param Throwable $throwable
40
     */
41
    public function add(Throwable $throwable) {
42
        $this->collection[] = $throwable;
43
    }
44
45
    /**
46
     * Determines, the current exception stack contains any exception
47
     *
48
     * @return bool
49
     */
50
    public function hasAny() {
51
        return ($this->count() > 0);
52
    }
53
54
    /**
55
     * {@inheritDoc}
56
     */
57
    public function getIterator() {
58
        return new ArrayIterator($this->collection);
59
    }
60
61
    /**
62
     * {@inheritDoc}
63
     */
64
    public function toArray() {
65
        return $this->collection;
66
    }
67
68
    /**
69
     * {@inheritDoc}
70
     */
71
    public function count() {
72
        return count($this->collection);
73
    }
74
75
}
76