Completed
Push — master ( 665986...1514e7 )
by Guilh
08:09 queued 05:04
created

DirectoryRouteLoader   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 52
Duplicated Lines 11.54 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 95.83%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 11
c 3
b 1
f 0
lcom 1
cbo 6
dl 6
loc 52
ccs 23
cts 24
cp 0.9583
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B load() 3 21 5
B supports() 3 12 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 Symfony\Component\Config\FileLocatorInterface;
15
use Symfony\Component\Config\Loader\Loader;
16
use Symfony\Component\Finder\Finder;
17
use Symfony\Component\Routing\RouteCollection;
18
19
/**
20
 * Parse annotated controller classes from all files of a directory.
21
 *
22
 * @author Christian Flothmann <[email protected]>
23
 */
24
class DirectoryRouteLoader extends Loader
25
{
26
    private $fileLocator;
27
    private $processor;
28
29 17
    public function __construct(FileLocatorInterface $fileLocator, RestRouteProcessor $processor)
30
    {
31 17
        $this->fileLocator = $fileLocator;
32 17
        $this->processor = $processor;
33 17
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 2
    public function load($resource, $type = null)
39
    {
40 2 View Code Duplication
        if (isset($resource[0]) && '@' === $resource[0]) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
41 1
            $resource = $this->fileLocator->locate($resource);
42 1
        }
43
44 2
        if (!is_dir($resource)) {
45
            throw new \InvalidArgumentException(sprintf('Given resource of type "%s" is no directory.', $resource));
46
        }
47
48 2
        $collection = new RouteCollection();
49
50 2
        $finder = new Finder();
51
52 2
        foreach ($finder->in($resource)->name('*.php')->files() as $file) {
53 2
            $imported = $this->processor->importResource($this, ClassUtils::findClassInFile($file), array(), null, null, 'rest');
54 2
            $collection->addCollection($imported);
55 2
        }
56
57 2
        return $collection;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63 16
    public function supports($resource, $type = null)
64
    {
65 16
        if ('rest' !== $type || !is_string($resource)) {
66 1
            return false;
67
        }
68
69 15 View Code Duplication
        if (isset($resource[0]) && '@' === $resource[0]) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
70 1
            $resource = $this->fileLocator->locate($resource);
71 1
        }
72
73 15
        return is_dir($resource);
74
    }
75
}
76