Completed
Branch 0.8-dev (5d9532)
by Kacper
02:49
created

ContextualToken   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 37
Duplicated Lines 37.84 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 68.18%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 6
c 5
b 0
f 0
lcom 1
cbo 2
dl 14
loc 37
ccs 15
cts 22
cp 0.6818
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B validate() 14 29 5

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
 * Highlighter
4
 *1
5
 * Copyright (C) 2016, Some right reserved.
6
 *
7
 * @author Kacper "Kadet" Donat <[email protected]>
8
 *
9
 * Contact with author:
10
 * Xmpp: [email protected]
11
 * E-mail: [email protected]
12
 *
13
 * From Kadet with love.
14
 */
15
16
namespace Kadet\Highlighter\Parser\Token;
17
18
use Kadet\Highlighter\Parser\Context;
19
use Kadet\Highlighter\Parser\Validator\Validator;
20
21
class ContextualToken extends Token
22
{
23 1
    public function __construct($name, $options = [])
24
    {
25 1
        parent::__construct($name, $options);
26 1
    }
27
28 1
    protected function validate(Context $context)
29
    {
30 1
        if ($context->language !== $this->rule->language) {
31 1
            $this->setValid(false);
32
33 1
            return false;
34
        }
35
36 1
        if (!$context->has($this->name)) {
37 1 View Code Duplication
            if (!$this->rule->validator->validate($context)) {
38 1
                $this->setValid(false);
39 1
            } else {
40
                $this->_valid       = true;
41
                $this->_end->_valid = false;
42
            }
43 1 View Code Duplication
        } else {
44
            if (!$this->rule->validator->validate($context, [ $this->name => Validator::CONTEXT_IN ])) {
45
                $this->setValid(false);
46
            } else {
47
                $this->_valid       = false;
48
                $this->_end->_valid = true;
49
            }
50
        }
51
52 1
        $this->_end->_start = false;
53 1
        $this->_end         = false;
54
55 1
        return true;
56
    }
57
}
58