Completed
Pull Request — master (#6)
by Tomáš
05:31
created

FlexibleExtensionFilesystemLoader   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 124
Duplicated Lines 12.9 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 4
dl 16
loc 124
ccs 0
cts 62
cp 0
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 20 2
A getSource() 8 8 1
B getCacheKey() 0 26 5
A isFresh() 8 8 1
A getSubscribedEvents() 0 6 1
A beforeRun() 0 8 2

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 a part of Sculpin.
5
 *
6
 * (c) Dragonfly Development Inc.
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 Symplify\PHP7_Sculpin\Bundle\TwigBundle;
13
14
use Symplify\PHP7_Sculpin\Core\Event\SculpinEvents;
15
use Symplify\PHP7_Sculpin\Core\Event\SourceSetEvent;
16
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
17
18
final class FlexibleExtensionFilesystemLoader implements \Twig_LoaderInterface, EventSubscriberInterface
19
{
20
    /**
21
     * @var FilesystemLoader
22
     */
23
    private $filesystemLoader;
24
25
    /**
26
     * @var array
27
     */
28
    private $cachedCacheKey = [];
29
30
    /**
31
     * @var array
32
     */
33
    private $cachedCacheKeyExtension = [];
34
35
    /**
36
     * @var array
37
     */
38
    private $cachedCacheKeyException = [];
39
40
    /**
41
     * Constructor.
42
     *
43
     * @param string   $sourceDir
44
     * @param string[] $sourcePaths
45
     * @param string[] $paths
46
     * @param string[] $extensions
47
     */
48
    public function __construct($sourceDir, array $sourcePaths, array $paths, array $extensions)
49
    {
50
        $mappedSourcePaths = array_map(function ($path) use ($sourceDir) {
51
            return $sourceDir.'/'.$path;
52
        }, $sourcePaths);
53
54
        $allPaths = array_merge(
55
            array_filter($mappedSourcePaths, function ($path) {
56
                return file_exists($path);
57
            }),
58
            array_filter($paths, function ($path) {
59
                return file_exists($path);
60
            })
61
        );
62
63
        $this->filesystemLoader = new FilesystemLoader($allPaths);
64
        $this->extensions = array_map(function ($ext) {
0 ignored issues
show
Bug introduced by
The property extensions does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
65
            return $ext ? '.'.$ext : $ext;
66
        }, $extensions);
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72 View Code Duplication
    public function getSource($name)
0 ignored issues
show
Duplication introduced by
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...
73
    {
74
        $this->getCacheKey($name);
75
76
        $extension = $this->cachedCacheKeyExtension[$name];
77
78
        return $this->filesystemLoader->getSource($name.$extension);
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function getCacheKey($name)
85
    {
86
        if (isset($this->cachedCacheKey[$name])) {
87
            $extension = $this->cachedCacheKeyExtension[$name];
88
89
            return $this->cachedCacheKey[$name] = $this->filesystemLoader->getCacheKey($name.$extension);
90
        }
91
92
        if (isset($this->cachedCacheKeyException[$name])) {
93
            throw $this->cachedCacheKeyException[$name];
94
        }
95
96
        foreach ($this->extensions as $extension) {
97
            try {
98
                $this->cachedCacheKey[$name] = $this->filesystemLoader->getCacheKey($name.$extension);
99
                $this->cachedCacheKeyExtension[$name] = $extension;
100
101
                return $this->cachedCacheKey[$name];
102
            } catch (\Twig_Error_Loader $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
103
            }
104
        }
105
106
        throw $this->cachedCacheKeyException[$name] = new \Twig_Error_Loader(
107
            sprintf('Template "%s" is not defined.', $name)
108
        );
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114 View Code Duplication
    public function isFresh($name, $time)
0 ignored issues
show
Duplication introduced by
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...
115
    {
116
        $this->getCacheKey($name);
117
118
        $extension = $this->cachedCacheKeyExtension[$name];
119
120
        return $this->filesystemLoader->isFresh($name.$extension, $time);
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126
    public static function getSubscribedEvents() : array
127
    {
128
        return [
129
            SculpinEvents::EVENT_BEFORE_RUN => 'beforeRun',
130
        ];
131
    }
132
133
    public function beforeRun(SourceSetEvent $sourceSetEvent)
134
    {
135
        if ($sourceSetEvent->sourceSet()->newSources()) {
136
            $this->cachedCacheKey = [];
137
            $this->cachedCacheKeyExtension = [];
138
            $this->cachedCacheKeyException = [];
139
        }
140
    }
141
}
142