Completed
Push — master ( f0854f...1f8ceb )
by Christian
07:23 queued 12s
created

RestRouteLoader::supports()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 3
1
<?php
2
3
/*
4
 * This file is part of the FOSRestBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FOS\RestBundle\Routing\Loader;
13
14
use FOS\RestBundle\Routing\Loader\Reader\RestControllerReader;
15
use Symfony\Bundle\FrameworkBundle\Controller\ControllerNameParser;
16
use Symfony\Component\Config\FileLocatorInterface;
17
use Symfony\Component\Config\Loader\Loader;
18
use Symfony\Component\DependencyInjection\ContainerInterface;
19
use Symfony\Component\HttpFoundation\Request;
20
use Symfony\Component\HttpKernel\Kernel;
21
22
/**
23
 * RestRouteLoader REST-enabled controller router loader.
24
 *
25
 * @author Konstantin Kudryashov <[email protected]>
26
 * @author Bulat Shakirzyanov <[email protected]>
27
 */
28
class RestRouteLoader extends Loader
29
{
30
    protected $container;
31
    protected $controllerParser;
32
    protected $controllerReader;
33
    protected $defaultFormat;
34
    protected $locator;
35
36
    /**
37
     * Initializes loader.
38
     *
39
     * @param ContainerInterface   $container
40
     * @param FileLocatorInterface $locator
41
     * @param RestControllerReader $controllerReader
42
     * @param string               $defaultFormat
43
     */
44 56
    public function __construct(
45
        ContainerInterface $container,
46
        FileLocatorInterface $locator,
47
        $controllerReader,
48
        $defaultFormat = 'html'
49
    ) {
50 56
        $this->container = $container;
51 56
        $this->locator = $locator;
52 56
53 56
        if ($controllerReader instanceof ControllerNameParser || null === $controllerReader) {
0 ignored issues
show
Bug introduced by
The class Symfony\Bundle\Framework...er\ControllerNameParser does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
54 56
            @trigger_error(sprintf('Not passing an instance of %s as the 3rd argument of %s() is deprecated since FOSRestBundle 2.8.', RestControllerReader::class, __METHOD__), E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
55 56
56
            $this->controllerParser = $controllerReader;
57
58 View Code Duplication
            if (!$defaultFormat instanceof RestControllerReader) {
59
                throw new \TypeError(sprintf('Argument 4 passed to %s() must be an instance of %s, %s given.', __METHOD__, RestControllerReader::class, is_object($defaultFormat) ? get_class($defaultFormat) : gettype($defaultFormat)));
0 ignored issues
show
Unused Code introduced by
The call to TypeError::__construct() has too many arguments starting with sprintf('Argument 4 pass...ettype($defaultFormat)).

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
60
            }
61
62 26
            $this->controllerReader = $defaultFormat;
63 1
            $this->defaultFormat = func_num_args() > 4 ? func_get_arg(4) : 'html';
64 26 View Code Duplication
        } elseif (!$controllerReader instanceof RestControllerReader) {
65
            throw new \TypeError(sprintf('Argument 3 passed to %s() must be an instance of %s, %s given.', __METHOD__, RestControllerReader::class, is_object($controllerReader) ? get_class($controllerReader) : gettype($controllerReader)));
0 ignored issues
show
Unused Code introduced by
The call to TypeError::__construct() has too many arguments starting with sprintf('Argument 3 pass...ype($controllerReader)).

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
66
        } else {
67
            $this->controllerReader = $controllerReader;
68
            $this->defaultFormat = $defaultFormat;
69
        }
70 37
    }
71
72 37
    /**
73
     * Returns controller reader.
74 37
     *
75 37
     * @return RestControllerReader
76 37
     */
77
    public function getControllerReader()
78 37
    {
79
        return $this->controllerReader;
80
    }
81
82
    /**
83
     * {@inheritdoc}
84 21
     */
85
    public function load($controller, $type = null)
86 21
    {
87 21
        list($prefix, $class) = $this->getControllerLocator($controller);
88 21
89 21
        $collection = $this->controllerReader->read(new \ReflectionClass($class));
90
        $collection->prependRouteControllersWithPrefix($prefix);
91
        $collection->setDefaultFormat($this->defaultFormat);
92
93
        return $collection;
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99
    public function supports($resource, $type = null)
100
    {
101 37
        return is_string($resource)
102
            && 'rest' === $type
103 37
            && !in_array(pathinfo($resource, PATHINFO_EXTENSION), ['xml', 'yml', 'yaml']
104 37
        );
105
    }
106 37
107
    /**
108
     * Returns controller locator by it's id.
109
     *
110
     * @param string $controller
111
     *
112
     * @throws \InvalidArgumentException
113
     *
114
     * @return array
115
     */
116
    private function getControllerLocator($controller)
117 37
    {
118
        $class = null;
119 1
        $prefix = null;
120 1
121 1
        if (0 === strpos($controller, '@')) {
122
            $file = $this->locator->locate($controller);
123
            $controllerClass = ClassUtils::findClassInFile($file);
0 ignored issues
show
Bug introduced by
It seems like $file defined by $this->locator->locate($controller) on line 122 can also be of type array; however, FOS\RestBundle\Routing\L...tils::findClassInFile() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
124
125 1
            if (false === $controllerClass) {
126 1
                throw new \InvalidArgumentException(sprintf('Can\'t find class for controller "%s"', $controller));
127
            }
128
129 37
            $controller = $controllerClass;
130
        }
131 37
132 37
        if ($this->container->has($controller)) {
133 37
            // service_id
134
            $prefix = $controller.':';
135
136
            if (Kernel::VERSION_ID >= 40100) {
137
                $prefix .= ':';
138
            }
139
140
            $useScope = method_exists($this->container, 'enterScope') && $this->container->hasScope('request');
0 ignored issues
show
Bug introduced by
The method hasScope() does not seem to exist on object<Symfony\Component...ion\ContainerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
141
            if ($useScope) {
142
                $this->container->enterScope('request');
0 ignored issues
show
Bug introduced by
The method enterScope() does not seem to exist on object<Symfony\Component...ion\ContainerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
143
                $this->container->set('request', new Request());
144
            }
145
            $class = get_class($this->container->get($controller));
146 37
            if ($useScope) {
147
                $this->container->leaveScope('request');
0 ignored issues
show
Bug introduced by
The method leaveScope() does not seem to exist on object<Symfony\Component...ion\ContainerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
148
            }
149
        } elseif (class_exists($controller)) {
150
            // full class name
151
            $class = $controller;
152 37
            $prefix = $class.'::';
153
        } elseif ($this->controllerParser && false !== strpos($controller, ':')) {
154
            // bundle:controller notation
155
            try {
156
                $notation = $this->controllerParser->parse($controller.':method');
157
                list($class) = explode('::', $notation);
158
                $prefix = $class.'::';
159
            } catch (\Exception $e) {
160
                throw new \InvalidArgumentException(sprintf('Can\'t locate "%s" controller.', $controller));
161
            }
162
        }
163
164
        if (empty($class)) {
165
            throw new \InvalidArgumentException(sprintf('Class could not be determined for Controller identified by "%s".', $controller));
166
        }
167
168
        return [$prefix, $class];
169
    }
170
}
171