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 ( b2a978...11290e )
by
unknown
12s
created

AbstractTable::setDefaultOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1.0007

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 10
cts 11
cp 0.9091
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 1
crap 1.0007
1
<?php
2
namespace CrossKnowledge\DataTableBundle\DataTable\Table;
3
4
use CrossKnowledge\DataTableBundle\DataTable\ColumnBuilder;
5
use CrossKnowledge\DataTableBundle\DataTable\Formatter\FormatterInterface;
6
use CrossKnowledge\DataTableBundle\DataTable\Table\Layout\Bootstrap;
7
use CrossKnowledge\DataTableBundle\DataTable\Table\Layout\DataTableLayoutInterface;
8
use CrossKnowledge\DataTableBundle\Table\Element\Column;
9
use Symfony\Component\Form\FormBuilder;
10
use Symfony\Component\Form\FormFactory;
11
use Symfony\Component\HttpFoundation\Request;
12
use Symfony\Component\OptionsResolver\OptionsResolver;
13
use Symfony\Component\Routing\Router;
14
use CrossKnowledge\DataTableBundle\DataTable\Request\PaginateRequest;
15
use Symfony\Component\Security\Core\Authorization\AuthorizationChecker;
16
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
17
18
abstract class AbstractTable
19
{
20
    const VIEW_CONTEXT   = 'view';
21
    
22
    /**
23
     * @var Router
24
     */
25
    protected $router;
26
    /**
27
     * @var PaginateRequest
28
     */
29
    protected $currentRequest;
30
    /**
31
     * @var FormFactory
32
     */
33
    protected $formFactory;
34
    /**
35
     * @var Form
36
     */
37
    protected $filterForm;
38
    /**
39
     * @var AuthorizationChecker
40
     */
41
    protected $authorizationChecker;
42
43
    /**
44
     * @var Column[]
45
     */
46
    protected $columns = array();
47
    protected $columnsInitialized = array();
48
49
    /**
50
     * @var OptionsResolver
51
     */
52
    protected $optionsResolver;
53
54
    /**
55
     * @var array Key value array of options
56
     */
57
    protected $options = [];
58
59
    /**
60
     * @var DataTableLayoutInterface
61
     */
62
    protected $layout;
63
    /**
64
     * @param FormFactory $formFactory
65
     * @param Router $router
66
     */
67 4
    public function __construct(
68
        FormFactory $formFactory,
69
        AuthorizationCheckerInterface $checker,
70
        Router $router,
71
        FormatterInterface $formatter,
72
        DataTableLayoutInterface $layout = null
73
    ) {
74 4
        $this->formFactory = $formFactory;
75 4
        $this->router = $router;
76 4
        $this->formatter = $formatter;
0 ignored issues
show
Bug introduced by
The property formatter 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...
77 4
        $this->authorizationChecker = $checker;
0 ignored issues
show
Documentation Bug introduced by
$checker is of type object<Symfony\Component...zationCheckerInterface>, but the property $authorizationChecker was declared to be of type object<Symfony\Component...n\AuthorizationChecker>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
78 4
        $this->layout = null === $layout ? new Bootstrap() : $layout;
79 4
        $this->optionsResolver = new OptionsResolver();
80
        //$this->initColumnsDefinitions();
81 4
        $this->setDefaultOptions($this->optionsResolver);
82 4
        $this->configureOptions($this->optionsResolver);
83 4
        $this->options = $this->optionsResolver->resolve();
84 4
    }
85
    /**
86
     *
87
     * Example implementation
88
89
    public function buildColumns(ColumnBuilder $builder)
90
    {
91
        $builder->add('Learner.FirstName', new Column('First name title', ['width' => '20%']))
92
                ->add('Learner.Name', new Column('Last name'));
93
    }
94
     *
95
     * @return array key must be the column field name,
96
     *               value must be an array of options for https://datatables.net/reference/option/columns
97
     */
98
    abstract public function buildColumns(ColumnBuilder $builder, $context = self::VIEW_CONTEXT);
99
    /**
100
     * Must return a \Traversable a traversable element that must contain for each element an ArrayAccess such as
101
     *      key(colname) => value(db value)
102
     *
103
     * The filter should be used there.
104
     *
105
     * Example of the expected return
106
    return [
107
        'first_name' => 'John',
108
        'last_name' => 'Doe'
109
    ];
110
     * Example:
111
    return new \PropelCollection();
112
     *
113
     * @return \Traversable
114
     */
115
    abstract public function getDataIterator();
116
    /**
117
     * @return int the total number of rows regardless of filters
118
     */
119
    abstract public function getUnfilteredCount();
120
    /**
121
     * @return int|false if there is no such count
122
     */
123
    abstract public function getFilteredCount();
124
125 4
    private final function setDefaultOptions(OptionsResolver $resolver)
0 ignored issues
show
Coding Style introduced by
As per PSR2, final should precede the visibility keyword.
Loading history...
126
    {
127 4
        $resolver->setDefaults([
128 4
            'layout' => $this->layout,
129 4
            'client_side_filtering' => false,
130 4
            'filter_reload_table_on_change' => false,
131 4
            'template' => 'CrossKnowledgeDataTableBundle::default_table.html.twig',
132 4
            'data_table_custom_options' => [],
133 4
            'has_filter_form' => function() {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
134
                return $this->getFilterForm()->count()>1;
135
            }
136 4
        ]);
137 4
    }
138
    /**
139
     * Configure the table options
140
     *
141
     * @param OptionsResolver $resolver
142
     */
143 4
    public function configureOptions(OptionsResolver $resolver){}
0 ignored issues
show
Unused Code introduced by
The parameter $resolver is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
144
    /**
145
     * @return array
146
     */
147 2
    public function setOptions(array $options)
148
    {
149 2
        $this->options = $this->optionsResolver->resolve(array_merge($this->options, $options));
150 2
    }
151
    /**
152
     * @return array
153
     */
154 2
    public function getOptions()
155
    {
156 2
        return $this->options;
157
    }
158
    /**
159
     * Build the filter form
160
     *
161
     * @param FormBuilder $builder
162
     *
163
     * @return FormBuilder
164
     */
165
    public function buildFilterForm(FormBuilder $builder)
166
    {
167
        return $builder;
168
    }
169
    /**
170
     * @return string
171
     */
172
    public function getAjaxAdditionalParameters()
173
    {
174
        return [];
175
    }
176
    /**
177
     * @return string[] should return the content to insert in the rows key(colname) => value(string / html / any)
178
     */
179 3
    public function getOutputRows($context = self::VIEW_CONTEXT)
180
    {
181 3
        $t = [];
182 3
        foreach ($this->getDataIterator() as $item) {
183 3
            $formatted = $this->formatter->formatRow($item,  $this, $context);
0 ignored issues
show
Coding Style introduced by
Expected 1 space instead of 2 after comma in function call.
Loading history...
184 3
            $t[] = $formatted;
185 3
        }
186
187 3
        return $t;
188
    }
189
    
190
    /**
191
     * @see getColumns() same as getColumns but filtered for datatable JS API
192
     */
193 2
    public function getClientSideColumns($context = self::VIEW_CONTEXT)
194
    {
195 2
        $columns = $this->getColumns($context);
196 2
        $clientSideCols = [];
197 2
        foreach ($columns as $colid=>$column) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space before "=>"; 0 found
Loading history...
Coding Style introduced by
Expected 1 space after "=>"; 0 found
Loading history...
198 2
            $clientSideCols[$colid] = $column->getClientSideDefinition();
199 2
        }
200
201 2
        return $clientSideCols;
202
    }
203
    /**
204
     * @param Request $request
205
     */
206 1
    public function handleRequest(Request $request)
207
    {
208 1
        $this->currentRequest = PaginateRequest::fromHttpRequest($request, $this);
209 1
    }
210
    /**
211
     * @return PaginateRequest
212
     */
213 1
    public function getCurrentRequest()
214
    {
215 1
        return $this->currentRequest;
216
    }
217
    /**
218
     * @return Form|\Symfony\Component\Form\Form
219
     */
220
    public function getFilterForm()
221
    {
222
        if (null===$this->filterForm) {
223
            $this->filterForm = $this->buildFilterForm(
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->buildFilterForm($..., 'button'))->getForm() of type object<Symfony\Component\Form\Form> is incompatible with the declared type object<CrossKnowledge\Da...e\DataTable\Table\Form> of property $filterForm.

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...
224
                $this->formFactory->createNamedBuilder($this->getTableId().'_filter')
0 ignored issues
show
Compatibility introduced by
$this->formFactory->crea...d('dofilter', 'button') of type object<Symfony\Component...m\FormBuilderInterface> is not a sub-type of object<Symfony\Component\Form\FormBuilder>. It seems like you assume a concrete implementation of the interface Symfony\Component\Form\FormBuilderInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
225
                    ->add('dofilter', 'button')
226
            )->getForm();
227
        }
228
229
        return $this->filterForm;
230
    }
231
    /**
232
     * @return array key value of variables accessible for renderers.
233
     */
234 2
    public function buildView()
235
    {
236
        $viewParameters = [
237 2
            'columns' => $this->getClientSideColumns(),
238 2
            'data'   => $this->getOutputRows(),
239 2
            'datatable' => $this,
240 2
            'unfilteredRowsCount' => $this->getUnfilteredCount(),
241 2
            'filteredRowsCount' => $this->getFilteredCount(),
242 2
        ];
243
244 2
        if ($this->getOptions()['has_filter_form']) {
245 2
            $viewParameters['filterForm'] = $this->getFilterForm()->createView();
246 2
        }
247
248 2
        return $viewParameters;
249
    }
250
251
    /**
252
     * Sets the formatter
253
     *
254
     * @param FormatterInterface $formatter
255
     *
256
     * @return void
257
     */
258
    public function setFormatter(FormatterInterface $formatter)
259
    {
260
        $this->formatter = $formatter;
261
    }
262
    /**
263
     * @return string a table idenfitier that will be used for ajax requests
264
     */
265
    public final function getTableId()
0 ignored issues
show
Coding Style introduced by
As per PSR2, final should precede the visibility keyword.
Loading history...
266
    {
267
        return $this->tableId;
0 ignored issues
show
Bug introduced by
The property tableId 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...
268
    }
269
    /**
270
     * @return \CrossKnowledge\DataTableBundle\Table\Element\Column[]
271
     */
272
    public function getColumns($context = self::VIEW_CONTEXT)
273
    {
274
        if(!array_key_exists($context, $this->columns)) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
275
            $this->initColumnsDefinitions($context);
276
        }
277
        return $this->columns[$context];
278
    }
279
    /**
280
     * Builds the columns definition
281
     */
282
    protected function initColumnsDefinitions($context = self::VIEW_CONTEXT)
283
    {
284
        $builder = new ColumnBuilder();
285
286
        $this->buildColumns($builder, $context);
287
288
        $this->columns[$context] = $builder->getColumns();
289
        $this->columnsInitialized[$context] =  true;
290
    }
291
    /**
292
     * Sets the table identifier
293
     *
294
     * @return null
295
     */
296 4
    public final function setTableId($id)
0 ignored issues
show
Coding Style introduced by
As per PSR2, final should precede the visibility keyword.
Loading history...
297
    {
298 4
        $this->tableId = $id;
299 4
    }
300
}
301