Issues (20)

Security Analysis    not enabled

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.

src/Eloquent/DescendantsRelation.php (2 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 namespace Arcanedev\LaravelNestedSet\Eloquent;
2
3
use Arcanedev\LaravelNestedSet\Utilities\NestedSet;
4
use Arcanedev\LaravelNestedSet\NodeTrait;
5
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
6
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Database\Eloquent\Relations\Relation;
9
use Illuminate\Database\Query\Builder;
10
use InvalidArgumentException;
11
12
/**
13
 * Class     DescendantsRelation
14
 *
15
 * @package  Arcanedev\LaravelNestedSet\Eloquent
16
 * @author   ARCANEDEV <[email protected]>
17
 *
18
 * @method  static  \Arcanedev\LaravelNestedSet\Eloquent\Collection  get(array $columns = ['*'])
19
 */
20
class DescendantsRelation extends Relation
21
{
22
    /* -----------------------------------------------------------------
23
     |  Properties
24
     | -----------------------------------------------------------------
25
     */
26
    /**
27
     * The Eloquent query builder instance.
28
     *
29
     * @var \Arcanedev\LaravelNestedSet\Eloquent\QueryBuilder
30
     */
31
    protected $query;
32
33
    /**
34
     * The parent model instance.
35
     *
36
     * @var \Arcanedev\LaravelNestedSet\NodeTrait
37
     */
38
    protected $parent;
39
40
    /* -----------------------------------------------------------------
41
     |  Constructor
42
     | -----------------------------------------------------------------
43
     */
44
    /**
45
     * DescendantsRelation constructor.
46
     *
47
     * @param  \Arcanedev\LaravelNestedSet\Eloquent\QueryBuilder  $builder
48
     * @param  \Illuminate\Database\Eloquent\Model|NodeTrait      $model
49
     */
50 36
    public function __construct(QueryBuilder $builder, Model $model)
51
    {
52
        // @codeCoverageIgnoreStart
53
        if ( ! NestedSet::isNode($model)) {
54
            throw new InvalidArgumentException('Model must be node.');
55
        }
56
        // @codeCoverageIgnoreEnd
57
58 36
        parent::__construct($builder, $model);
59 36
    }
60
61
    /* -----------------------------------------------------------------
62
     |  Main Methods
63
     | -----------------------------------------------------------------
64
     */
65
    /**
66
     * Add the constraints for a relationship query.
67
     *
68
     * @param  \Illuminate\Database\Eloquent\Builder  $query
69
     * @param  \Illuminate\Database\Eloquent\Builder  $parent
70
     * @param  array|mixed                            $columns
71
     *
72
     * @return \Illuminate\Database\Eloquent\Builder
73
     */
74 3
    public function getRelationExistenceQuery(
75
        EloquentBuilder $query,
76
        EloquentBuilder $parent,
77
        $columns = ['*']
78
    ) {
79 3
        $query->select($columns);
0 ignored issues
show
The method select() does not exist on Illuminate\Database\Eloquent\Builder. Did you maybe mean createSelectWithConstraint()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
80
81 3
        $table = $query->getModel()->getTable();
82 3
        $hash  = $this->getRelationCountHash();
83
84 3
        $query->from("$table as $hash");
85
86 3
        $grammar = $query->getQuery()->getGrammar();
87
88 3
        $table = $grammar->wrapTable($table);
89 3
        $hash  = $grammar->wrapTable($hash);
90 3
        $lft   = $grammar->wrap($this->parent->getLftName());
91 3
        $rgt   = $grammar->wrap($this->parent->getRgtName());
92
93 3
        return $query->whereRaw("{$hash}.{$lft} between {$table}.{$lft} + 1 and {$table}.{$rgt}");
94
    }
95
96
    /**
97
     * @param  \Illuminate\Database\Eloquent\Builder  $query
98
     * @param  \Illuminate\Database\Eloquent\Builder  $parent
99
     * @param  array|mixed                            $columns
100
     *
101
     * @return \Illuminate\Database\Eloquent\Builder
102
     */
103
    public function getRelationQuery(
104
        EloquentBuilder $query,
105
        EloquentBuilder $parent,
106
        $columns = ['*']
107
    ) {
108
        return $this->getRelationExistenceQuery($query, $parent, $columns);
109
    }
110
111
    /**
112
     * Get a relationship join table hash.
113
     *
114
     * @return string
115
     */
116 3
    public function getRelationCountHash()
117
    {
118 3
        return 'self_'.md5(microtime(true));
119
    }
120
121
    /**
122
     * Set the base constraints on the relation query.
123
     */
124 36
    public function addConstraints()
125
    {
126 36
        if ( ! static::$constraints) return;
127
128 30
        $this->query->whereDescendantOf($this->parent);
129 30
    }
130
131
    /**
132
     * Set the constraints for an eager load of the relation.
133
     *
134
     * @param  array  $models
135
     */
136
    public function addEagerConstraints(array $models)
137
    {
138 3
        $this->query->whereNested(function (Builder $inner) use ($models) {
0 ignored issues
show
Documentation Bug introduced by
The method whereNested does not exist on object<Arcanedev\Laravel...\Eloquent\QueryBuilder>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
139
            // We will use this query in order to apply constraints to the base query builder
140 3
            $outer = $this->parent->newQuery();
141
142 3
            foreach ($models as $model) {
143 3
                $outer->setQuery($inner)->orWhereDescendantOf($model);
144 1
            }
145 3
        });
146 3
    }
147
148
    /**
149
     * Initialize the relation on a set of models.
150
     *
151
     * @param  array   $models
152
     * @param  string  $relation
153
     *
154
     * @return array
155
     */
156 3
    public function initRelation(array $models, $relation)
157
    {
158 3
        return $models;
159
    }
160
161
    /**
162
     * Match the eagerly loaded results to their parents.
163
     *
164
     * @param  array                                     $models
165
     * @param  \Illuminate\Database\Eloquent\Collection  $results
166
     * @param  string                                    $relation
167
     *
168
     * @return array
169
     */
170 3
    public function match(array $models, EloquentCollection $results, $relation)
171
    {
172 3
        foreach ($models as $model) {
173
            /** @var  \Illuminate\Database\Eloquent\Model  $model */
174 3
            $model->setRelation(
175 3
                $relation, $this->getDescendantsForModel($model, $results)
176 1
            );
177 1
        }
178
179 3
        return $models;
180
    }
181
182
    /**
183
     * Get the results of the relationship.
184
     *
185
     * @return mixed
186
     */
187 3
    public function getResults()
188
    {
189 3
        return $this->query->get();
190
    }
191
192
    /**
193
     * @param  \Illuminate\Database\Eloquent\Model       $model
194
     * @param  \Illuminate\Database\Eloquent\Collection  $results
195
     *
196
     * @return \Arcanedev\LaravelNestedSet\Eloquent\Collection|\Illuminate\Database\Eloquent\Collection
197
     */
198 3
    protected function getDescendantsForModel(Model $model, EloquentCollection $results)
199
    {
200 3
        $result = $this->related->newCollection();
201
202 3
        foreach ($results as $descendant) {
203 3
            if ($descendant->isDescendantOf($model)) {
204 3
                $result->push($descendant);
205 1
            }
206 1
        }
207
208 3
        return $result;
209
    }
210
}
211