CannotAlterCollection   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 65
rs 10
c 0
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A byOverWriting() 0 7 1
A byAddingTo() 0 5 1
A byResizingTo() 0 8 1
A byRemoving() 0 7 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stratadox\ImmutableCollection;
6
7
use BadMethodCallException;
8
use function count;
9
use function get_class;
10
use function sprintf;
11
use Stratadox\Collection\Collection;
12
use Stratadox\Collection\NotAllowed;
13
14
/**
15
 * Exception to notify the client code that the collection may not be mutated.
16
 *
17
 * Although an immutable collection may provide functions to allow altering its
18
 * state, such methods will yield an altered copy instead of mutating the
19
 * original.
20
 * Some built-in syntax, ie.
21
 * ```php
22
 * unset($collection[$position])
23
 * ```
24
 * or
25
 * ```php
26
 * $collection[$position] = $newValue;
27
 * ```
28
 * would mutate the internal state if allowed. This exception is thrown to
29
 * prevent such mutating behaviour.
30
 *
31
 * @package Stratadox\Collection
32
 * @author  Stratadox
33
 */
34
final class CannotAlterCollection extends BadMethodCallException implements NotAllowed
35
{
36
    /**
37
     * Indicates that this append operation is not allowed.
38
     *
39
     * @param Collection $collection The collection that may not be mutated.
40
     * @return NotAllowed            The exception object.
41
     */
42
    public static function byAddingTo(Collection $collection): NotAllowed
43
    {
44
        return new static(sprintf(
45
            'Cannot add to the immutable collection `%s`. ',
46
            get_class($collection)
47
        ));
48
    }
49
50
    /**
51
     * Indicates that this write operation is not allowed.
52
     *
53
     * @param Collection $collection The collection that may not be mutated.
54
     * @param int        $index      The index that will not be overwritten.
55
     * @return NotAllowed            The exception object.
56
     */
57
    public static function byOverWriting(Collection $collection, int $index): NotAllowed
58
    {
59
        return new static(sprintf(
60
            'Cannot write to the immutable collection `%s`. ' .
61
            '(Tried writing to position %d).',
62
            get_class($collection),
63
            $index
64
        ));
65
    }
66
67
    /**
68
     * Indicates that this unset operation is not allowed.
69
     *
70
     * @param Collection $collection The collection that may not be mutated.
71
     * @param int        $index      The index that will not be removed.
72
     * @return NotAllowed            The exception object.
73
     */
74
    public static function byRemoving(Collection $collection, int $index): NotAllowed
75
    {
76
        return new static(sprintf(
77
            'Cannot alter the immutable collection `%s`. ' .
78
            '(Tried to unset position %d).',
79
            get_class($collection),
80
            $index
81
        ));
82
    }
83
84
    /**
85
     * Indicates that this resize operation is not allowed.
86
     *
87
     * @param Collection $collection The collection that may not be mutated.
88
     * @param int        $size       The size that will not be assigned.
89
     * @return NotAllowed            The exception object.
90
     */
91
    public static function byResizingTo(Collection $collection, int $size): NotAllowed
92
    {
93
        return new static(sprintf(
94
            'Cannot directly resize the immutable collection `%s`. ' .
95
            '(Tried to resize from %d to %d).',
96
            get_class($collection),
97
            count($collection),
98
            $size
99
        ));
100
    }
101
}
102