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.

EsLint   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 108
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
B check() 34 34 7
B file() 37 37 8
A extractContent() 14 14 2
A buildEslintJsFile() 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 EsLint 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('eslint');
32
        self::file($parameters);
33
34
        $excludes = [];
35
        if (true === array_key_exists('eslint_exclude', $parameters)) {
36
            foreach ($parameters['eslint_exclude'] as $key => $exclude) {
37
                $excludes[$key] = $parameters['eslint_path'] . '/' . $exclude;
38
            }
39
        }
40
41
        $errors = [];
42
        foreach ($files as $file) {
43
            if (false === self::exist($file, $parameters['eslint_path'], 'js') || in_array($file, $excludes, true)) {
44
                continue;
45
            }
46
47
            $process = new Process(
48
                sprintf('eslint %s -c %s/.eslintrc.js', $file, self::location($parameters)),
49
                $parameters['root_directory']
50
            );
51
            $process->run();
52
            if (!$process->isSuccessful()) {
53
                $errors[] = new Error(
54
                    $file,
55
                    sprintf('<error>%s</error>', trim($process->getErrorOutput())),
56
                    sprintf('<error>%s</error>', trim($process->getOutput()))
57
                );
58
            }
59
        }
60
61
        return $errors;
62
    }
63
64
    public static function file($parameters) : void
65
    {
66
        $jsContent = file_get_contents(__DIR__ . '/../.eslintrc.js.dist');
67
68
        $arrayContent = self::extractContent($jsContent);
69
        foreach ($parameters['eslint_rules'] as $ruleType => $rules) {
70
            if (!is_array($rules)) {
71
                $arrayContent[$ruleType] = $rules;
72
                continue;
73
            }
74
75
            if (self::isAssociativeArray($rules)) {
76
                foreach ($rules as $name => $rule) {
77
                    $arrayContent[$ruleType][$name] = $rule;
78
                }
79
                continue;
80
            }
81
82
            foreach ($rules as $rule) {
83
                if (in_array($rule, $arrayContent[$ruleType], true)) {
84
                    continue;
85
                }
86
                $arrayContent[$ruleType][] = $rule;
87
            }
88
        }
89
90
        $location = self::location($parameters) . '/.eslintrc.js';
91
        $fileSystem = new Filesystem();
92
93
        try {
94
            $fileSystem->remove($location);
95
            $fileSystem->touch($location);
96
            file_put_contents($location, self::buildEslintJsFile($arrayContent));
97
        } catch (\Exception $exception) {
98
            echo sprintf("Something wrong happens during the creating process: \n%s\n", $exception->getMessage());
99
        }
100
    }
101
102
    private static function extractContent($jsFileContent) : array
103
    {
104
        $position = mb_strpos($jsFileContent, 'module.exports = ');
105
        $position = $position + 17;
106
        $json = mb_substr($jsFileContent, $position);
107
        $json = rtrim(trim($json), ';');
108
109
        $result = json_decode($json, true);
110
        if (null === $result) {
111
            throw new JsonParserErrorException();
112
        }
113
114
        return $result;
115
    }
116
117
    private static function buildEslintJsFile(array $content) : string
118
    {
119
        return sprintf('module.exports = %s;', str_replace('\/', '/', json_encode($content)));
120
    }
121
122
    private static function isAssociativeArray(array $array)
123
    {
124
        return [] !== $array && array_keys($array) !== range(0, count($array) - 1);
125
    }
126
127
    private static function location($parameters) : string
128
    {
129
        return $parameters['root_directory'] . '/' . $parameters['eslint_file_location'];
130
    }
131
}
132