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 (#18)
by
unknown
02:03
created

AbstractTable::getCurrentRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
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
    public function __construct(
68
        FormFactory $formFactory,
69
        AuthorizationCheckerInterface $checker,
70
        Router $router,
71
        FormatterInterface $formatter,
72
        DataTableLayoutInterface $layout = null
73
    ) {
74
        $this->formFactory = $formFactory;
75
        $this->router = $router;
76
        $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
        $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
        $this->layout = null === $layout ? new Bootstrap() : $layout;
79
        $this->optionsResolver = new OptionsResolver();
80
        //$this->initColumnsDefinitions();
81
        $this->setDefaultOptions($this->optionsResolver);
82
        $this->configureOptions($this->optionsResolver);
83
        $this->options = $this->optionsResolver->resolve();
84
    }
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
    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
        $resolver->setDefaults([
128
            'layout' => $this->layout,
129
            'client_side_filtering' => false,
130
            'filter_reload_table_on_change' => false,
131
            'no_init_loading' => false,
132
            'template' => 'CrossKnowledgeDataTableBundle::default_table.html.twig',
133
            'data_table_custom_options' => [],
134
            'has_filter_form' => function() {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
135
                return $this->getFilterForm()->count()>1;
136
            }
137
        ]);
138
    }
139
    /**
140
     * Configure the table options
141
     *
142
     * @param OptionsResolver $resolver
143
     */
144
    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...
145
    /**
146
     * @return array
147
     */
148
    public function setOptions(array $options)
149
    {
150
        $this->options = $this->optionsResolver->resolve(array_merge($this->options, $options));
151
    }
152
    /**
153
     * @return array
154
     */
155
    public function getOptions()
156
    {
157
        return $this->options;
158
    }
159
    /**
160
     * Build the filter form
161
     *
162
     * @param FormBuilder $builder
163
     *
164
     * @return FormBuilder
165
     */
166
    public function buildFilterForm(FormBuilder $builder)
167
    {
168
        return $builder;
169
    }
170
    /**
171
     * @return string
172
     */
173
    public function getAjaxAdditionalParameters()
174
    {
175
        return [];
176
    }
177
    /**
178
     * @return string[] should return the content to insert in the rows key(colname) => value(string / html / any)
179
     */
180
    public function getOutputRows($context = self::VIEW_CONTEXT)
181
    {
182
        $t = [];
183
        foreach ($this->getDataIterator() as $item) {
184
            $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...
185
            $t[] = $formatted;
186
        }
187
188
        return $t;
189
    }
190
    
191
    /**
192
     * @see getColumns() same as getColumns but filtered for datatable JS API
193
     */
194
    public function getClientSideColumns($context = self::VIEW_CONTEXT)
195
    {
196
        $columns = $this->getColumns($context);
197
        $clientSideCols = [];
198
        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...
199
            $clientSideCols[$colid] = $column->getClientSideDefinition();
200
        }
201
202
        return $clientSideCols;
203
    }
204
    /**
205
     * @param Request $request
206
     */
207
    public function handleRequest(Request $request)
208
    {
209
        $this->currentRequest = PaginateRequest::fromHttpRequest($request, $this);
210
    }
211
    /**
212
     * @return PaginateRequest
213
     */
214
    public function getCurrentRequest()
215
    {
216
        return $this->currentRequest;
217
    }
218
    /**
219
     * @return Form|\Symfony\Component\Form\Form
220
     */
221
    public function getFilterForm()
222
    {
223
        if (null===$this->filterForm) {
224
            $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...
225
                $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...
226
                    ->add('dofilter', 'button')
227
            )->getForm();
228
        }
229
230
        return $this->filterForm;
231
    }
232
    /**
233
     * Sets the formatter
234
     *
235
     * @param FormatterInterface $formatter
236
     *
237
     * @return void
238
     */
239
    public function setFormatter(FormatterInterface $formatter)
240
    {
241
        $this->formatter = $formatter;
242
    }
243
    /**
244
     * @return string a table idenfitier that will be used for ajax requests
245
     */
246
    public final function getTableId()
0 ignored issues
show
Coding Style introduced by
As per PSR2, final should precede the visibility keyword.
Loading history...
247
    {
248
        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...
249
    }
250
    /**
251
     * @return \CrossKnowledge\DataTableBundle\Table\Element\Column[]
252
     */
253
    public function getColumns($context = self::VIEW_CONTEXT)
254
    {
255
        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...
256
            $this->initColumnsDefinitions($context);
257
        }
258
        return $this->columns[$context];
259
    }
260
    /**
261
     * Builds the columns definition
262
     */
263
    protected function initColumnsDefinitions($context = self::VIEW_CONTEXT)
264
    {
265
        $builder = new ColumnBuilder();
266
267
        $this->buildColumns($builder, $context);
268
269
        $this->columns[$context] = $builder->getColumns();
270
        $this->columnsInitialized[$context] =  true;
271
    }
272
    /**
273
     * Sets the table identifier
274
     *
275
     * @return null
276
     */
277
    public final function setTableId($id)
0 ignored issues
show
Coding Style introduced by
As per PSR2, final should precede the visibility keyword.
Loading history...
278
    {
279
        $this->tableId = $id;
280
    }
281
}
282