Component   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 49
Duplicated Lines 32.65 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 16
loc 49
rs 10
c 0
b 0
f 0
wmc 6
lcom 0
cbo 1

1 Method

Rating   Name   Duplication   Size   Complexity  
B enqueueAssets() 16 46 6

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 Flynt\Features\Components;
4
5
use Flynt\Utils\Asset;
6
use Flynt\Utils\StringHelpers;
7
8
class Component
9
{
10
    public static function enqueueAssets($componentName, array $dependencies = [])
11
    {
12
        // register dependencies
13
        foreach ($dependencies as $dependency) {
14
            // TODO add a warning if the same script is loaded several times (with different names) in multiple components
15
            Asset::register($dependency);
16
        }
17
18
        // collect script dependencies
19
        $scriptDeps = array_reduce($dependencies, function ($list, $dependency) {
20
            if ($dependency['type'] === 'script') {
21
                array_push($list, $dependency['name']);
22
            }
23
            return $list;
24
        }, ['jquery']); // jquery as a default dependency
25
26
        // Enqueue Component Scripts if they exist
27
        $scriptAbsPath = Asset::requirePath("Components/{$componentName}/script.js");
28 View Code Duplication
        if (is_file($scriptAbsPath)) {
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...
29
            Asset::enqueue([
30
                'type' => 'script',
31
                'name' => "Flynt/Components/{$componentName}",
32
                'path' => "Components/{$componentName}/script.js",
33
                'dependencies' => $scriptDeps
34
            ]);
35
        }
36
37
        // collect style dependencies
38
        $styleDeps = array_reduce($dependencies, function ($list, $dependency) {
39
            if ($dependency['type'] === 'style') {
40
                array_push($list, $dependency['name']);
41
            }
42
            return $list;
43
        }, []);
44
45
        // Enqueue Component Styles if they exist
46
        $styleAbsPath = Asset::requirePath("Components/{$componentName}/style.css");
47 View Code Duplication
        if (is_file($styleAbsPath)) {
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...
48
            Asset::enqueue([
49
                'type' => 'style',
50
                'name' => "Flynt/Components/{$componentName}",
51
                'path' => "Components/{$componentName}/style.css",
52
                'dependencies' => $styleDeps
53
            ]);
54
        }
55
    }
56
}
57