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.
Completed
Push — master ( 4c329c...42f2f9 )
by Beñat
02:15
created

Stylelint::file()   C

Complexity

Conditions 8
Paths 28

Size

Total Lines 37
Code Lines 23

Duplication

Lines 37
Ratio 100 %

Importance

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