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 ( 46962f...4c329c )
by Beñat
02:26
created

EsLint::extractContent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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