Passed
Push — master ( 463105...84cd59 )
by Roberto
04:47
created

ExceptionCollection   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
c 0
b 0
f 0
lcom 1
cbo 0
dl 0
loc 72
ccs 0
cts 38
cp 0
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setExceptions() 0 8 2
A add() 0 6 1
A count() 0 4 1
A getIterator() 0 4 1
A getFirst() 0 4 2
A __toString() 0 7 1
1
<?php
2
3
namespace NFePHP\Common\Exception;
4
5
use IteratorAggregate;
6
use Countable;
7
8
class ExceptionCollection extends \Exception implements ExceptionInterface, IteratorAggregate, Countable
9
{
10
    protected $exceptions = array();
11
    private $shortMessage;
12
    
13
    public function __construct($message = '', $code = 0, \Exception $previous = null)
14
    {
15
        parent::__construct($message, $code, $previous);
16
        $this->shortMessage = $message;
17
    }
18
    
19
    /**
20
     * Set all of the exceptions
21
     * @param array $exceptions Array of exceptions
22
     * @return ExceptionCollection;
0 ignored issues
show
Documentation introduced by
The doc-type ExceptionCollection; could not be parsed: Expected "|" or "end of type", but got ";" at position 19. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
23
     */
24
    public function setExceptions(array $exceptions)
25
    {
26
        $this->exceptions = array();
27
        foreach ($exceptions as $exception) {
28
            $this->add($exception);
29
        }
30
        return $this;
31
    }
32
    
33
    /**
34
     * Add exceptions to the collection
35
     * @param ExceptionCollection|\Exception $e Exception to add
0 ignored issues
show
Bug introduced by
There is no parameter named $e. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
36
     * @return ExceptionCollection;
0 ignored issues
show
Documentation introduced by
The doc-type ExceptionCollection; could not be parsed: Expected "|" or "end of type", but got ";" at position 19. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
37
     */
38
    public function add(\Exception $exception)
39
    {
40
        $this->exceptions[] = $exception;
41
        $this->message = $this->__toString();
42
        return $this;
43
    }
44
    
45
    /**
46
     * Get the total number of request exceptions
47
     * @return int
48
     */
49
    public function count()
50
    {
51
        return count($this->exceptions);
52
    }
53
    
54
    /**
55
     * Allows array-like iteration over the request exceptions
56
     * @return \ArrayIterator
57
     */
58
    public function getIterator()
59
    {
60
        return new \ArrayIterator($this->exceptions);
61
    }
62
    
63
    /**
64
     * Get the first exception in the collection
65
     * @return \Exception
66
     */
67
    public function getFirst()
68
    {
69
        return $this->exceptions ? $this->exceptions[0] : null;
70
    }
71
    
72
    public function __toString()
73
    {
74
        $messages = array_map(function (\Exception $exception) {
75
            return $exception->getMessage();
76
        }, $this->exceptions);
77
        return implode(PHP_EOL, $messages);
78
    }
79
}
80