GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Stylelint   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 110
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
B check() 36 36 7
B file() 37 37 8
A extractContent() 14 14 2
A buildStylelintJsFile() 4 4 1
A isAssociativeArray() 4 4 2
A location() 4 4 1

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 CS library.
5
 *
6
 * Copyright (c) 2015-present LIN3S <[email protected]>
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
declare(strict_types=1);
13
14
namespace LIN3S\CS\Checker;
15
16
use LIN3S\CS\Error\Error;
17
use LIN3S\CS\Exception\JsonParserErrorException;
18
use Symfony\Component\Filesystem\Filesystem;
19
use Symfony\Component\Process\Process;
20
21
/**
22
 * @author Beñat Espiña <[email protected]>
23
 */
24 View Code Duplication
final class Stylelint implements Checker
0 ignored issues
show
Duplication introduced by
This class 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...
25
{
26
    use FileFinder;
27
    use ToolAvailability;
28
29
    public static function check(array $files = [], array $parameters = null)
30
    {
31
        self::isAvailable('stylelint');
32
        self::file($parameters);
33
34
        $excludes = [];
35
        if (true === array_key_exists('stylelint_exclude', $parameters)) {
36
            foreach ($parameters['stylelint_exclude'] as $key => $exclude) {
37
                $excludes[$key] = $parameters['stylelint_path'] . '/' . $exclude;
38
            }
39
        }
40
41
        $errors = [];
42
        foreach ($files as $file) {
43
            if (false === self::exist($file, $parameters['stylelint_path'], 'scss')
44
                || in_array($file, $excludes, true)
45
            ) {
46
                continue;
47
            }
48
49
            $process = new Process(
50
                sprintf('stylelint %s -c %s/.stylelintrc.js', $file, self::location($parameters)),
51
                $parameters['root_directory']
52
            );
53
            $process->run();
54
            if (!$process->isSuccessful()) {
55
                $errors[] = new Error(
56
                    $file,
57
                    sprintf('<error>%s</error>', trim($process->getErrorOutput())),
58
                    sprintf('<error>%s</error>', trim($process->getOutput()))
59
                );
60
            }
61
        }
62
63
        return $errors;
64
    }
65
66
    public static function file($parameters) : void
67
    {
68
        $jsContent = file_get_contents(__DIR__ . '/../.stylelintrc.js.dist');
69
70
        $arrayContent = self::extractContent($jsContent);
71
        foreach ($parameters['stylelint_rules'] as $ruleType => $rules) {
72
            if (!is_array($rules)) {
73
                $arrayContent[$ruleType] = $rules;
74
                continue;
75
            }
76
77
            if (self::isAssociativeArray($rules)) {
78
                foreach ($rules as $name => $rule) {
79
                    $arrayContent[$ruleType][$name] = $rule;
80
                }
81
                continue;
82
            }
83
84
            foreach ($rules as $rule) {
85
                if (in_array($rule, $arrayContent[$ruleType], true)) {
86
                    continue;
87
                }
88
                $arrayContent[$ruleType][] = $rule;
89
            }
90
        }
91
92
        $location = self::location($parameters) . '/.stylelintrc.js';
93
        $fileSystem = new Filesystem();
94
95
        try {
96
            $fileSystem->remove($location);
97
            $fileSystem->touch($location);
98
            file_put_contents($location, self::buildStylelintJsFile($arrayContent));
99
        } catch (\Exception $exception) {
100
            echo sprintf("Something wrong happens during the creating process: \n%s\n", $exception->getMessage());
101
        }
102
    }
103
104
    private static function extractContent($jsFileContent) : array
105
    {
106
        $position = mb_strpos($jsFileContent, 'module.exports = ');
107
        $position = $position + 17;
108
        $json = mb_substr($jsFileContent, $position);
109
        $json = rtrim(trim($json), ';');
110
111
        $result = json_decode($json, true);
112
        if (null === $result) {
113
            throw new JsonParserErrorException();
114
        }
115
116
        return $result;
117
    }
118
119
    private static function buildStylelintJsFile(array $content) : string
120
    {
121
        return sprintf('module.exports = %s;', str_replace('\/', '/', json_encode($content)));
122
    }
123
124
    private static function isAssociativeArray(array $array)
125
    {
126
        return [] !== $array && array_keys($array) !== range(0, count($array) - 1);
127
    }
128
129
    private static function location($parameters) : string
130
    {
131
        return $parameters['root_directory'] . '/' . $parameters['stylelint_file_location'];
132
    }
133
}
134