Issues (28)

Security Analysis    no request data  

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

lib/GridViewFactory.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Psi\Component\Grid;
6
7
use Psi\Component\Grid\Metadata\GridMetadata;
8
use Psi\Component\Grid\View\Grid as GridView;
9
use Psi\Component\ObjectAgent\AgentInterface;
10
use Psi\Component\ObjectAgent\Query\Composite;
11
use Psi\Component\ObjectAgent\Query\Query;
12
13
class GridViewFactory
14
{
15
    /**
16
     * @var ColumnFactory
17
     */
18
    private $columnFactory;
19
20
    /**
21
     * @var FilterFactory
22
     */
23
    private $filterFactory;
24
25
    public function __construct(
26
        ColumnFactory $columnFactory,
27
        FilterBarFactoryInterface $filterFactory,
28
        QueryFactory $queryFactory
29
    ) {
30
        $this->columnFactory = $columnFactory;
31
        $this->filterFactory = $filterFactory;
0 ignored issues
show
Documentation Bug introduced by
It seems like $filterFactory of type object<Psi\Component\Gri...terBarFactoryInterface> is incompatible with the declared type object<Psi\Component\Grid\FilterFactory> of property $filterFactory.

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...
32
        $this->queryFactory = $queryFactory;
0 ignored issues
show
The property queryFactory 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...
33
    }
34
35
    public function createView(AgentInterface $agent, GridContext $gridContext, GridMetadata $gridMetadata): GridView
36
    {
37
        // create the filter form based on the metadata and submit any data.
38
        $filterForm = $this->filterFactory->createForm($gridMetadata, $agent->getCapabilities());
39
40
        if ($gridContext->getFilter()) {
41
            $filterForm->submit($gridContext->getFilter());
42
43
            if (false === $filterForm->isValid()) {
44
                foreach ($filterForm->getErrors(true) as $name => $error) {
45
                    $message[] = sprintf(
0 ignored issues
show
Coding Style Comprehensibility introduced by
$message was never initialized. Although not strictly required by PHP, it is generally a good practice to add $message = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
46
                        '%s %s',
47
                        $error->getOrigin()->getPropertyPath(),
48
                        $error->getMessage()
49
                    );
50
                }
51
52
                throw new \InvalidArgumentException(sprintf(
53
                    'Invalid filter form: ' . implode(', ', $message)
54
                ));
55
            }
56
        }
57
58
        $criteria = $this->filterFactory->createExpression($gridMetadata, $filterForm->getData());
59
60
        if ($criteria instanceof Composite && empty($criteria->getExpressions())) {
61
            $criteria = null;
62
        }
63
64
        $criteria = [
65
            'criteria' => $criteria,
66
            'orderings' => $this->resolveOrderings($gridContext->getOrderings(), $gridMetadata),
67
            'firstResult' => $gridContext->getPageOffset(),
68
            'maxResults' => $gridContext->isPaginated() ? $gridContext->getPageSize() : null,
69
        ];
70
71
        if ($gridMetadata->hasQuery()) {
72
            $query = $this->queryFactory->createQuery($agent->getCanonicalClassFqn($gridContext->getClassFqn()), $gridMetadata->getQuery());
73
            $criteria['selects'] = $query->getSelects();
74
            $criteria['joins'] = $query->getJoins();
75
76
            if ($query->hasExpression()) {
77
                if (null === $criteria['criteria']) {
78
                    $criteria['criteria'] = $query->getExpression();
79
                } else {
80
                    // filter and user criterias need to be combined
81
                    $criteria['criteria'] = new Composite(Composite::AND, [$query->getExpression(), $criteria['criteria']]);
82
                }
83
            }
84
        }
85
86
        // create the query and get the data collection from the object-agent.
87
        $query = Query::create($gridContext->getClassFqn(), $criteria);
88
        $collection = new \IteratorIterator($agent->query($query));
89
90
        return new View\Grid(
91
            $gridContext->getClassFqn(),
92
            $gridMetadata->getName(),
93
            new View\Table($this->columnFactory, $gridMetadata, $gridContext, $collection, $gridContext),
0 ignored issues
show
The call to Table::__construct() has too many arguments starting with $gridContext.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
94
            new View\Paginator($gridContext, count($collection), $this->getNumberOfRecords($agent, $query)),
95
            new View\FilterBar($filterForm->createView(), $gridContext),
96
            new View\ActionBar($gridMetadata)
97
        );
98
    }
99
100
    private function getNumberOfRecords(AgentInterface $agent, Query $query)
101
    {
102
        if (false === $agent->getCapabilities()->canQueryCount()) {
103
            return;
104
        }
105
106
        return $agent->queryCount($query);
107
    }
108
109
    private function resolveOrderings(array $orderings, GridMetadata $metadata)
110
    {
111
        $columns = $metadata->getColumns();
112
        $realOrderings = [];
113
114
        foreach ($orderings as $columnName => $direction) {
115
            if (!isset($columns[$columnName])) {
116
                throw new \RuntimeException(sprintf(
117
                    'Invalid column "%s"', $columnName
118
                ));
119
            }
120
121
            $column = $columns[$columnName];
122
            $options = $column->getOptions();
123
124
            if (!isset($options['sort_field'])) {
125
                $options['sort_field'] = $columnName;
126
            }
127
128
            $realOrderings[$options['sort_field']] = $direction;
129
        }
130
131
        return $realOrderings;
132
    }
133
}
134