Completed
Push — master ( 5f2f2b...044d29 )
by Denis
69:27 queued 42:33
created

AttributeColumn   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 58
Duplicated Lines 6.9 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 4
loc 58
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 3
B renderValue() 4 30 8

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 Woo\GridView\Columns;
4
5
use Woo\GridView\Exceptions\ColumnRenderException;
6
use Woo\GridView\Exceptions\GridViewConfigException;
7
8
class AttributeColumn extends BaseColumn
9
{
10
    /**
11
     * @var string - allowed: url, email, text, image
12
     */
13
    public $contentFormat = 'text';
14
15
    public function __construct($config)
16
    {
17
        if (is_string($config)) {
18
            $config = [
19
                'value' => $config,
20
            ];
21
        }
22
23
        parent::__construct($config);
24
25
        if (empty($this->title)) {
26
            $this->title = ucfirst(str_replace('_', ' ', $this->value));
27
        }
28
    }
29
30
    /**
31
     * @inheritdoc
32
     * @throws ColumnRenderException
33
     * @throws GridViewConfigException
34
     */
35
    public function renderValue($row)
36
    {
37
        $value = '';
38
39
        if (is_array($row)) {
40
41
            if (isset($row[$this->value])) {
42
                $value = $row[$this->value];
43
            }
44
        } elseif (isset($row->{$this->value})) {
45
            $value = $row->{$this->value};
46
        }
47
48
        switch ($this->contentFormat) {
49
            case 'text':
50
                return htmlentities($value);
51
52 View Code Duplication
            case 'url':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
53
                return '<a href="' . htmlspecialchars($value, ENT_QUOTES) . '">' . htmlentities($value) . '</a>';
54
55 View Code Duplication
            case 'email':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
56
                return '<a href="mailto:' . htmlspecialchars($value, ENT_QUOTES) . '">' . htmlentities($value) . '</a>';
57
                
58
            case 'image':
59
                return '<img src="' . htmlspecialchars($value, ENT_QUOTES) . '">';
60
                
61
            default:
62
                throw new GridViewConfigException('Invalid content format for attribute collumn: ' . $this->value);
63
        }
64
    }
65
}