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.

RulesParser   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 90
Duplicated Lines 13.33 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 12
loc 90
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A toArray() 0 4 1
A parse() 12 12 2
A getRules() 0 8 2
A getColumn() 0 6 1
A getAttributes() 0 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
namespace Milkmeowo\Framework\Repository\Generators\Migrations;
4
5
use Illuminate\Contracts\Support\Arrayable;
6
7
class RulesParser implements Arrayable
8
{
9
    /**
10
     * The set of rules.
11
     *
12
     * @var string
13
     */
14
    protected $rules;
15
16
    /**
17
     * Create new instance.
18
     *
19
     * @param string|null $rules
20
     */
21
    public function __construct($rules = null)
22
    {
23
        $this->rules = $rules;
24
    }
25
26
    /**
27
     * Convert string migration to array.
28
     *
29
     * @return array
30
     */
31
    public function toArray()
32
    {
33
        return $this->parse($this->rules);
34
    }
35
36
    /**
37
     * Parse a string to array of formatted rules.
38
     *
39
     * @param  string $rules
40
     *
41
     * @return array
42
     */
43 View Code Duplication
    public function parse($rules)
0 ignored issues
show
Duplication introduced by
This method 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...
44
    {
45
        $this->rules = $rules;
46
        $parsed = [];
47
        foreach ($this->getRules() as $rulesArray) {
48
            $column = $this->getColumn($rulesArray);
49
            $attributes = $this->getAttributes($column, $rulesArray);
50
            $parsed[$column] = $attributes;
51
        }
52
53
        return $parsed;
54
    }
55
56
    /**
57
     * Get array of rules.
58
     *
59
     * @return array
60
     */
61
    public function getRules()
62
    {
63
        if (is_null($this->rules)) {
64
            return [];
65
        }
66
67
        return explode(',', str_replace(' ', '', $this->rules));
68
    }
69
70
    /**
71
     * Get column name from rules.
72
     *
73
     * @param  string $rules
74
     *
75
     * @return string
76
     */
77
    public function getColumn($rules)
78
    {
79
        return array_first(explode('=>', $rules), function ($key, $value) {
80
            return $value;
81
        });
82
    }
83
84
    /**
85
     * Get column attributes.
86
     *
87
     * @param  string $column
88
     * @param  string $rules
89
     *
90
     * @return array
91
     */
92
    public function getAttributes($column, $rules)
93
    {
94
        return str_replace($column.'=>', '', $rules);
95
    }
96
}
97