Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
ContainerException
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
2 / 2
3
100.00% covered (success)
100.00%
1 / 1
 forCircularDependency
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 forInvalidService
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3declare(strict_types=1);
4
5/**
6 * This file is part of php-fast-forward/container.
7 *
8 * This source file is subject to the license bundled
9 * with this source code in the file LICENSE.
10 *
11 * @copyright Copyright (c) 2025-2026 Felipe SayĆ£o Lobato Abreu <github@mentordosnerds.com>
12 * @license   https://opensource.org/licenses/MIT MIT License
13 *
14 * @see       https://github.com/php-fast-forward/container
15 * @see       https://github.com/php-fast-forward
16 * @see       https://datatracker.ietf.org/doc/html/rfc2119
17 */
18
19namespace FastForward\Container\Exception;
20
21use Exception;
22use Throwable;
23use Psr\Container\ContainerExceptionInterface;
24
25/**
26 * Exception type for container-related errors while resolving services.
27 *
28 * This class MUST be used to signal problems occurring during service resolution
29 * from a container that complies with PSR-11.
30 */
31 class ContainerException extends Exception implements ContainerExceptionInterface
32{
33    /**
34     * Creates an exception describing the active dependency cycle.
35     *
36     * @param list<string> $path the service identifiers, including the repeated entry
37     *
38     * @return self an exception preserving the complete resolution path
39     */
40    public static function forCircularDependency(array $path): self
41    {
42        return new self(\sprintf('Circular dependency: %s.', implode(' -> ', $path)));
43    }
44
45    /**
46     * Creates an exception for an invalid or unresolvable service identifier.
47     *
48     * This factory method MUST be invoked when a service ID fails to resolve
49     * or the resolved service is invalid in the current context.
50     *
51     * @param string $id the identifier of the service that caused the failure
52     * @param Throwable $previous the previous exception thrown during resolution
53     *
54     * @return self an instance of ContainerException describing the issue
55     */
56    public static function forInvalidService(string $id, Throwable $previous): self
57    {
58        $code = $previous->getCode();
59
60        return new self(\sprintf('Invalid service "%s".', $id), \is_int($code) ? $code : 0, $previous);
61    }
62}