Completed
Push — master ( a6d8dc...680f8e )
by Jan-Petter
03:57
created

RequestRate   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 70
Duplicated Lines 11.43 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 4
Bugs 3 Features 0
Metric Value
wmc 9
c 4
b 3
f 0
lcom 1
cbo 1
dl 8
loc 70
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A export() 0 4 2
A add() 0 17 4
A render() 8 8 2

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
namespace vipnytt\RobotsTxtParser\Parser\Directives;
3
4
use vipnytt\RobotsTxtParser\Parser\RobotsTxtInterface;
5
use vipnytt\RobotsTxtParser\Parser\Toolbox;
6
7
/**
8
 * Class RequestRate
9
 *
10
 * @package vipnytt\RobotsTxtParser\Parser\Directives
11
 */
12
class RequestRate implements DirectiveInterface, RobotsTxtInterface
13
{
14
    use Toolbox;
15
16
    /**
17
     * Directive
18
     */
19
    const DIRECTIVE = self::DIRECTIVE_REQUEST_RATE;
20
21
    /**
22
     * RequestRate array
23
     * @var array
24
     */
25
    protected $array = [];
26
27
    /**
28
     * RequestRate constructor.
29
     */
30
    public function __construct()
31
    {
32
    }
33
34
    /**
35
     * Add
36
     *
37
     * @param string $line
38
     * @return bool
39
     */
40
    public function add($line)
41
    {
42
        $array = preg_split('/\s+/', $line, 2);
43
        $result = [
44
            'rate' => $this->draftParseRate($array[0]),
45
        ];
46
        if ($result['rate'] === false) {
47
            return false;
48
        } elseif (
49
            !empty($array[1]) &&
50
            ($times = $this->draftParseTime($array[1])) !== false
51
        ) {
52
            $result = array_merge($result, $times);
53
        }
54
        $this->array[] = $result;
55
        return true;
56
    }
57
58
    /**
59
     * Export rules
60
     *
61
     * @return string[][]
62
     */
63
    public function export()
64
    {
65
        return empty($this->array) ? [] : [self::DIRECTIVE => $this->array];
66
    }
67
68
    /**
69
     * Render
70
     *
71
     * @return string[]
72
     */
73 View Code Duplication
    public function render()
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...
74
    {
75
        $result = [];
76
        foreach ($this->array as $value) {
77
            $result[] = self::DIRECTIVE . ': ' . $value;
78
        }
79
        return $result;
80
    }
81
}
82