Passed
Push — PSR-11-2 ( dfb1a3...d7a3e8 )
by Nikolaos
04:33
created

Exception::missingParameter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
/**
4
 * This file is part of the Phalcon Framework.
5
 *
6
 * (c) Phalcon Team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.txt
9
 * file that was distributed with this source code.
10
 *
11
 * Implementation of this file has been influenced by AuraPHP
12
 *
13
 * @link    https://github.com/auraphp/Aura.Di
14
 * @license https://github.com/auraphp/Aura.Di/blob/4.x/LICENSE
15
 */
16
17
declare(strict_types=1);
18
19
namespace Phalcon\Container;
20
21
use Exception as BaseException;
22
use Phalcon\Container\Exception\ContainerLocked;
23
use Phalcon\Container\Exception\MissingParameter;
24
use Phalcon\Container\Exception\MutationDoesNotImplementInterface;
25
use Phalcon\Container\Exception\NoSuchProperty;
26
use Phalcon\Container\Exception\ServiceNotFound;
27
use Phalcon\Container\Exception\ServiceNotObject;
28
use Phalcon\Container\Exception\SetterMethodNotFound;
29
use Psr\Container\ContainerExceptionInterface;
30
31
use function gettype;
32
use function is_object;
33
34
/**
35
 * Class Exception
36
 *
37
 * @package Phalcon\Container
38
 */
39
class Exception extends BaseException implements ContainerExceptionInterface
40
{
41
    /**
42
     * The container is locked and cannot be modified.
43
     *
44
     * @return ContainerLocked
45
     * @throws ContainerLocked
46
     */
47 1
    public static function containerLocked(): ContainerLocked
48
    {
49 1
        throw new ContainerLocked(
50 1
            "Cannot modify container when locked."
51
        );
52
    }
53
54
    /**
55
     * A class constructor param was not defined.
56
     *
57
     * @param string $class The class name.
58
     * @param string $parameter The constructor param name.
59
     *
60
     * @return MissingParameter
61
     */
62 4
    public static function missingParameter(
63
        string $class,
64
        string $parameter
65
    ): MissingParameter {
66 4
        throw new MissingParameter(
67 4
            "Parameter missing: " . $class . "::\$" . $parameter
68
        );
69
    }
70
71
    /**
72
     * The container does not have a requested service.
73
     *
74
     * @param string $service The service name.
75
     *
76
     * @return ServiceNotFound
77
     */
78 1
    public static function serviceNotFound(string $service): ServiceNotFound
79
    {
80 1
        throw new ServiceNotFound(
81 1
            "Service not defined: '" . $service . "'"
82
        );
83
    }
84
85
    /**
86
     * A setter method was defined, but it not available on the class.
87
     *
88
     * @param string $class  The class name.
89
     * @param string $method The method name.
90
     *
91
     * @return SetterMethodNotFound
92
     */
93 1
    public static function setterMethodNotFound(
94
        string $class,
95
        string $method
96
    ): SetterMethodNotFound {
97 1
        throw new SetterMethodNotFound(
98 1
            "Setter method not found: " . $class . "::" . $method . "()"
99
        );
100
    }
101
102
    /**
103
     * A mutation was lazy and returned a value that is not an instanceof
104
     * MutationInterface.
105
     *
106
     * @param mixed $value The returned value.
107
     *
108
     * @return MutationDoesNotImplementInterface>
109
     */
110 2
    public static function mutationDoesNotImplementInterface($value): MutationDoesNotImplementInterface
111
    {
112 2
        if (is_object($value)) {
113 1
            $className = get_class($value);
114 1
            throw new MutationDoesNotImplementInterface(
115 1
                "Mutation does not implement interface: " . $className
116
            );
117
        }
118
119 1
        $typeName = gettype($value);
120 1
        throw new MutationDoesNotImplementInterface(
121 1
            "Expected Mutation interface, got: " . $typeName
122
        );
123
    }
124
125
    /**
126
     * A requested property does not exist.
127
     *
128
     * @param string $name The property name.
129
     *
130
     * @return NoSuchProperty
131
     * @throws NoSuchProperty
132
     */
133 1
    public static function noSuchProperty(string $name): NoSuchProperty
134
    {
135 1
        throw new NoSuchProperty("Property does not exist: \$" . $name);
136
    }
137
}
138