Completed
Branch 2.0-dev (d250b8)
by Jan-Petter
03:02
created

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