Test Failed
Push — master ( a58c94...1bcec0 )
by Brent
03:26
created

TemplatePlugin   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 149
Duplicated Lines 23.49 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
dl 35
loc 149
rs 10
c 0
b 0
f 0
wmc 15
lcom 0
cbo 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A meta() 0 19 4
B css() 15 31 4
B js() 20 34 5
A image() 0 15 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
namespace Brendt\Stitcher\Template;
4
5
use brendt\image\ResponsiveFactory;
6
use Brendt\Stitcher\Config;
7
use Brendt\Stitcher\Factory\ParserFactory;
8
use Symfony\Component\Filesystem\Filesystem;
9
10
/**
11
 * This class provides functionality which can be used by template plugins/functions.
12
 */
13
class TemplatePlugin {
14
15
    /**
16
     * This function will read meta configuration from `meta` and output the corresponding meta tags.
17
     *
18
     * @return string
19
     */
20
    public function meta() {
21
        $meta = Config::get('meta');
22
23
        if (!is_array($meta)) {
24
            $meta = [$meta];
25
        }
26
27
        $result = [];
28
29
        foreach ($meta as $name => $content) {
30
            if (!is_string($content)) {
31
                continue;
32
            }
33
34
            $result[] = "<meta name=\"{$name}\" content=\"{$content}\">";
35
        }
36
37
        return implode("\n", $result);
38
    }
39
40
    /**
41
     * This function will take a source path and an optional inline parameter.
42
     * The CSS file will be copied from the source path to the public directory.
43
     * If the `minify` option is set to true in config.yml, the output will be minified.
44
     *
45
     * If the inline parameter is set, the output won't be copied to a public file,
46
     * but instead be outputted to an HTML string which can be included in a template.
47
     *
48
     * Files with the .scss and .sass extensions will be compiled to normal CSS files.
49
     *
50
     * @param string $src
51
     * @param bool   $inline
52
     *
53
     * @return string
54
     */
55
    public function css($src, $inline = false) {
56
        /** @var ParserFactory $factory */
57
        $factory = Config::getDependency('factory.parser');
58
59
        $parser = $factory->getParser($src);
60
        $data = $parser->parse($src);
61
62
        if (Config::get('minify')) {
63
            /** @var \CSSmin $minifier */
64
            $minifier = Config::getDependency('engine.minify.css');
65
            $data = $minifier->run($data);
66
        }
67
68 View Code Duplication
        if ($inline) {
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...
69
            $result = "<style>{$data}</style>";
70
        } else {
71
            $publicDir = Config::get('directories.public');
72
            $srcParsed = preg_replace('/\.scss|\.sass/', '.css', $src);
73
            $fs = new Filesystem();
74
            $dst = "{$publicDir}/$srcParsed";
75
76
            if ($fs->exists($dst)) {
77
                $fs->remove($dst);
78
            }
79
80
            $fs->dumpFile($dst, $data);
81
            $result = "<link rel=\"stylesheet\" type=\"text/css\" href=\"{$srcParsed}\">";
82
        }
83
84
        return $result;
85
    }
86
87
    /**
88
     * This function will take a source path and an optional inline parameter.
89
     * The JS file will be copied from the source path to the public directory.
90
     * If the `minify` option is set to true in config.yml, the output will be minified.
91
     *
92
     * If the inline parameter is set, the output won't be copied to a public file,
93
     * but instead be outputted to an HTML string which can be included in a template.
94
     *
95
     * @param string $src
96
     * @param bool   $inline
97
     * @param bool   $async
98
     *
99
     * @return string
100
     */
101
    public function js($src, $inline = false, $async = false) {
102
        /** @var ParserFactory $factory */
103
        $factory = Config::getDependency('factory.parser');
104
105
        $parser = $factory->getParser($src);
106
        $data = $parser->parse($src);
107
108
        if (Config::get('minify')) {
109
            $data = \JSMin::minify($data);
110
        }
111
112 View Code Duplication
        if ($inline) {
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...
113
            $result = "<script>{$data}</script>";
114
        } else {
115
            $publicDir = Config::get('directories.public');
116
            $fs = new Filesystem();
117
            $dst = "{$publicDir}/$src";
118
119
            if ($fs->exists($dst)) {
120
                $fs->remove($dst);
121
            }
122
123
            $fs->dumpFile($dst, $data);
124
            $result = "<script src=\"{$src}\"";
125
126
            if ($async) {
127
                $result .= ' async';
128
            }
129
130
            $result .= "></script>";
131
        }
132
133
        return $result;
134
    }
135
136
    /**
137
     * Create a responsive image using brendt\responsive-images.
138
     *
139
     * @param $src
140
     *
141
     * @return array
142
     *
143
     * @see \brendt\image\ResponsiveFactory
144
     */
145
    public function image($src) {
146
        /** @var ResponsiveFactory $factory */
147
        $factory = Config::getDependency('factory.image');
148
        $image = $factory->create($src);
149
150
        if (!$image) {
151
            return ['src' => null, 'srcset' => null, 'sizes' => null];
152
        }
153
154
        return [
155
            'src'    => $image->src(),
156
            'srcset' => $image->srcset(),
157
            'sizes'  => $image->sizes(),
158
        ];
159
    }
160
161
}
162