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
Push — master ( 49d6e8...d5b92a )
by Cyril
12s
created

Column::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 1
rs 9.4285
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
    /**
10
     * Column field info that might be used client side
11
     *
12
     * https://datatables.net/reference/option/columns
13
     *
14
     * @var array
15
     */
16
    public static $clientSideColumnOptions = [
17
        'cellType','className','contentPadding', 'createdCell', 'data', 'defaultContent', 'name', 'orderable',
18
        'orderData', 'orderDataType', 'render', 'searchable', 'title', 'type', 'visible', 'width'
19
    ];
20
21
    /**
22
     * @var string key/value of options
23
     */
24
    protected $options;
25
26
    /**
27
     * @var OptionsResolver
28
     */
29
    protected $optionsResolver;
30
31
    /**
32
     * @var \Closure callback that will be used to format this cell values
33
     */
34
    protected $formatValueCallback;
35
36 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...
37
    {
38 14
        $this->optionsResolver = new OptionsResolver();
39 14
        $this->configureOptions($this->optionsResolver);
40 14
        $this->setOptions(array_merge($options, ['title' => $title]));
41 13
    }
42
    /**
43
     * Column unique identifier
44
     * @param string $identifier
45
     * @return Column
46
     */
47 2
    public function setIdentifier($identifier)
48
    {
49 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...
50 2
        return $this;
51
    }
52
    /**
53
     * @param OptionsResolver $resolver
54
     */
55 14
    public function configureOptions(OptionsResolver $resolver)
56
    {
57 14
        $resolver->setDefined('title');
58 14
        $resolver->setDefault('auto_escape', true);
59 14
        $resolver->setDefined(static::$clientSideColumnOptions);
60 14
    }
61
    /**
62
     * @param array $options one within static::$clientSideColumnOptions
63
     */
64 14
    public function setOptions(array $options)
65
    {
66 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...
67 13
        return $this;
68
    }
69
    /**
70
     * @return mixed
71
     */
72 8
    public function getOptions()
73
    {
74 8
        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...
75
    }
76
    /**
77
     * @param string $title
78
     * @return Column
79
     */
80
    public function setTitle($title)
81
    {
82
        $this->options['title'] = $title;
83
        return $this;
84
    }
85
86
    /**
87
     * Format a cell content for this column
88
     * @param $value
89
     * @param array $rowData
90
     * @param $context
91
     * @return mixed
92
     */
93 8
    public function formatCell($value, array $rowData, $context)
94
    {
95 8
        if (is_callable($this->formatValueCallback)) {
96 1
            return call_user_func_array($this->formatValueCallback, [$value, $rowData]);
97
        } else{
0 ignored issues
show
Coding Style introduced by
Expected 1 space after ELSE keyword; 0 found
Loading history...
98 8
            return $value;
99
        }
100
    }
101
    
102
    /**
103
     * @return \Closure
104
     */
105 1
    public function getFormatValueCallback()
106
    {
107 1
        return $this->formatValueCallback;
108
    }
109
    /**
110
     * @param \Closure $callback
111
     */
112 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...
113
    {
114 2
        $this->formatValueCallback = $callback;
115 2
        return $this;
116
    }
117
    /**
118
     * @return string key/value filtered for client side API
119
     */
120 2
    public function getClientSideDefinition()
121
    {
122 2
        $infos = [];
123
124
        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...
125 2
            if (in_array($optname, static::$clientSideColumnOptions)) {
126 2
                $infos[$optname] = $optval;
127 2
            }
128 2
        });
129
130 2
        return $infos;
131
    }
132
}
133