Passed
Pull Request — master (#9)
by Pavel
05:20
created

Routing/Exception/FileLoaderLoadException.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * Copyright (c) 2010-2017 Fabien Potencier
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is furnished
11
 * to do so, subject to t * *he following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in all
14
 * copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
 * THE SOFTWARE.
23
 *
24
 */
25
26
namespace Bankiru\Api\Rpc\Routing\Exception;
27
28
class FileLoaderLoadException extends \RuntimeException implements FileLoaderException
29
{
30
    /**
31
     * @param string     $resource       The resource that could not be imported
32
     * @param string     $sourceResource The original resource importing the new resource
33
     * @param int        $code           The error code
34
     * @param \Exception $previous       A previous exception
35
     */
36
    public function __construct($resource, $sourceResource = null, $code = null, $previous = null)
37
    {
38
        $message = '';
39
        if ($previous) {
40
            // Include the previous exception, to help the user see what might be the underlying cause
41
42
            // Trim the trailing period of the previous message. We only want 1 period remove so no rtrim...
43
            if ('.' === substr($previous->getMessage(), -1)) {
44
                $trimmedMessage = substr($previous->getMessage(), 0, -1);
45
                $message        .= sprintf('%s', $trimmedMessage) . ' in ';
46
            } else {
47
                $message .= sprintf('%s', $previous->getMessage()) . ' in ';
48
            }
49
            $message .= $resource . ' ';
50
51
            // show tweaked trace to complete the human readable sentence
52
            if (null === $sourceResource) {
53
                $message .= sprintf('(which is loaded in resource "%s")', $this->varToString($resource));
54
            } else {
55
                $message .= sprintf('(which is being imported from "%s")', $this->varToString($sourceResource));
56
            }
57
            $message .= '.';
58
            // if there's no previous message, present it the default way
59
        } elseif (null === $sourceResource) {
60
            $message .= sprintf('Cannot load resource "%s".', $this->varToString($resource));
61
        } else {
62
            $message .= sprintf(
63
                'Cannot import resource "%s" from "%s".',
64
                $this->varToString($resource),
65
                $this->varToString($sourceResource)
66
            );
67
        }
68
69
        // Is the resource located inside a bundle?
70
        if ('@' === $resource[0]) {
71
            $parts   = explode(DIRECTORY_SEPARATOR, $resource);
72
            $bundle  = substr($parts[0], 1);
73
            $message .= sprintf(
74
                ' Make sure the "%s" bundle is correctly registered and loaded in the application kernel class.',
75
                $bundle
76
            );
77
            $message .= sprintf(
78
                ' If the bundle is registered, make sure the bundle path "%s" is not empty.',
79
                $resource
80
            );
81
        }
82
83
        parent::__construct($message, $code, $previous);
84
    }
85
86 View Code Duplication
    protected function varToString($var)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
87
    {
88
        if (is_object($var)) {
89
            return sprintf('Object(%s)', get_class($var));
90
        }
91
92
        if (is_array($var)) {
93
            $a = [];
94
            foreach ($var as $k => $v) {
95
                $a[] = sprintf('%s => %s', $k, $this->varToString($v));
96
            }
97
98
            return sprintf('Array(%s)', implode(', ', $a));
99
        }
100
101
        if (is_resource($var)) {
102
            return sprintf('Resource(%s)', get_resource_type($var));
103
        }
104
105
        if (null === $var) {
106
            return 'null';
107
        }
108
109
        if (false === $var) {
110
            return 'false';
111
        }
112
113
        if (true === $var) {
114
            return 'true';
115
        }
116
117
        return (string)$var;
118
    }
119
}
120