Completed
Branch 2.0-dev (131e57)
by Jan-Petter
02:08
created

CrawlDelay   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 78
Duplicated Lines 8.97 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 1
cbo 1
dl 7
loc 78
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A add() 0 16 4
A export() 0 4 2
A render() 7 7 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\Core\Directives;
3
4
use vipnytt\RobotsTxtParser\Core\RobotsTxtInterface;
5
use vipnytt\RobotsTxtParser\Core\Toolbox;
6
7
/**
8
 * Class CrawlDelay
9
 *
10
 * @package vipnytt\RobotsTxtParser\Core\Directives
11
 */
12
class CrawlDelay implements DirectiveInterface, RobotsTxtInterface
13
{
14
    use Toolbox;
15
16
    /**
17
     * Directive alternatives
18
     */
19
    const DIRECTIVE = [
20
        self::DIRECTIVE_CACHE_DELAY,
21
        self::DIRECTIVE_CRAWL_DELAY,
22
    ];
23
24
    /**
25
     * Directive
26
     */
27
    protected $directive = self::DIRECTIVE_CRAWL_DELAY;
28
29
    /**
30
     * Delay
31
     * @var float|int
32
     */
33
    protected $value;
34
35
    /**
36
     * CrawlDelay constructor.
37
     * @param string $directive
38
     */
39
    public function __construct($directive = self::DIRECTIVE_CRAWL_DELAY)
40
    {
41
        $this->directive = $this->validateDirective($directive, self::DIRECTIVE);
42
    }
43
44
    /**
45
     * Add
46
     *
47
     * @param float|int|string $line
48
     * @return bool
49
     */
50
    public function add($line)
51
    {
52
        if (
53
            !is_numeric($line) ||
54
            (
55
                isset($this->value) &&
56
                $this->value > 0
57
            )
58
        ) {
59
            return false;
60
        }
61
        // PHP hack to convert numeric string to float or int
62
        // http://stackoverflow.com/questions/16606364/php-cast-string-to-either-int-or-float
63
        $this->value = $line + 0;
64
        return true;
65
    }
66
67
    /**
68
     * Export rules
69
     *
70
     * @return float[]|int[]|string[]
71
     */
72
    public function export()
73
    {
74
        return empty($this->value) ? [] : [$this->directive => $this->value];
75
    }
76
77
    /**
78
     * Render
79
     *
80
     * @return string[]
81
     */
82 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...
83
    {
84
        if (!empty($this->value)) {
85
            return [$this->directive . ':' . $this->value];
86
        }
87
        return [];
88
    }
89
}
90