GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#7)
by
unknown
03:03
created

Column   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 89.47%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 12
c 3
b 0
f 0
lcom 2
cbo 1
dl 0
loc 129
ccs 34
cts 38
cp 0.8947
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A setIdentifier() 0 5 1
A configureOptions() 0 7 1
A setOptions() 0 5 1
A getOptions() 0 4 1
A setTitle() 0 5 1
A formatCell() 0 8 2
A getFormatValueCallback() 0 4 1
A setFormatValueCallback() 0 5 1
A getClientSideDefinition() 0 12 2
1
<?php
2
3
namespace CrossKnowledge\DataTableBundle\DataTable\Table\Element\Column;
4
5
use Symfony\Component\OptionsResolver\OptionsResolver;
6
7
class Column implements ColumnInterface
8
{
9
    const TYPE = 'string';
10
11
    /**
12
     * Column field info that might be used client side
13
     *
14
     * https://datatables.net/reference/option/columns
15
     *
16
     * @var array
17
     */
18
    public static $clientSideColumnOptions = [
19
        'cellType','className','contentPadding', 'createdCell', 'data', 'defaultContent', 'name', 'orderable',
20
        'orderData', 'orderDataType', 'render', 'searchable', 'title', 'type', 'visible', 'width'
21
    ];
22
23
    /**
24
     * @var string key/value of options
25
     */
26
    protected $options;
27
28
    /**
29
     * @var OptionsResolver
30
     */
31
    protected $optionsResolver;
32
33
    /**
34
     * @var \Closure callback that will be used to format this cell values
35
     */
36
    protected $formatValueCallback;
37
38 14
    public function __construct($title='', $options=[])
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$title" and equals sign; expected 1 but found 0
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$title"; expected 1 but found 0
Loading history...
Coding Style introduced by
Incorrect spacing between argument "$options" and equals sign; expected 1 but found 0
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$options"; expected 1 but found 0
Loading history...
39
    {
40 14
        $this->optionsResolver = new OptionsResolver();
41 14
        $this->configureOptions($this->optionsResolver);
42 14
        $this->setOptions(array_merge($options, ['title' => $title]));
43 13
    }
44
    /**
45
     * Column unique identifier
46
     * @param string $identifier
47
     * @return Column
48
     */
49 2
    public function setIdentifier($identifier)
50
    {
51 2
        $this->identifier = $identifier;
0 ignored issues
show
Bug introduced by
The property identifier does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
52 2
        return $this;
53
    }
54
    /**
55
     * @param OptionsResolver $resolver
56
     */
57 14
    public function configureOptions(OptionsResolver $resolver)
58
    {
59 14
        $resolver->setDefined('title');
60 14
        $resolver->setDefault('auto_escape', true);
61 14
        $resolver->setDefault('type', static::TYPE);
62 14
        $resolver->setDefined(static::$clientSideColumnOptions);
63 14
    }
64
    /**
65
     * @param array $options one within static::$clientSideColumnOptions
66
     */
67 14
    public function setOptions(array $options)
68
    {
69 14
        $this->options = $this->optionsResolver->resolve($options);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->optionsResolver->resolve($options) of type array is incompatible with the declared type string of property $options.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
70 13
        return $this;
71
    }
72
    /**
73
     * @return mixed
74
     */
75 4
    public function getOptions()
76
    {
77 4
        return $this->options;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->options; (string) is incompatible with the return type declared by the interface CrossKnowledge\DataTable...mnInterface::getOptions of type string[].

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
78
    }
79
    /**
80
     * @param string $title
81
     * @return Column
82
     */
83
    public function setTitle($title)
84
    {
85
        $this->options['title'] = $title;
86
        return $this;
87
    }
88
89
    /**
90
     * Format a cell content for this column
91
     * @param $value
92
     * @param array $rowData
93
     * @param $context
94
     * @return mixed
95
     */
96 3
    public function formatCell($value, array $rowData, $context)
97
    {
98 3
        if (is_callable($this->formatValueCallback)) {
99
            return call_user_func_array($this->formatValueCallback, [$value, $rowData]);
100
        } else{
0 ignored issues
show
Coding Style introduced by
Expected 1 space after ELSE keyword; 0 found
Loading history...
101 3
            return $value;
102
        }
103
    }
104
105
    /**
106
     * @return \Closure
107
     */
108 1
    public function getFormatValueCallback()
109
    {
110 1
        return $this->formatValueCallback;
111
    }
112
    /**
113
     * @param \Closure $callback
114
     */
115 2
    public function setFormatValueCallback(\Closure $callback=null)
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$callback" and equals sign; expected 1 but found 0
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$callback"; expected 1 but found 0
Loading history...
116
    {
117 2
        $this->formatValueCallback = $callback;
118 2
        return $this;
119
    }
120
    /**
121
     * @return string key/value filtered for client side API
122
     */
123 2
    public function getClientSideDefinition()
124
    {
125 2
        $infos = [];
126
127
        array_walk($this->options, function($optval, $optname) use (&$infos) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
128 2
            if (in_array($optname, static::$clientSideColumnOptions)) {
129 2
                $infos[$optname] = $optval;
130 2
            }
131 2
        });
132
133 2
        return $infos;
134
    }
135
}
136