Issues (34)

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.

src/Laravel/Presenter.php (10 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 Sofa\Revisionable\Laravel;
2
3
use Illuminate\Database\Eloquent\Collection;
4
use Illuminate\Database\Eloquent\Model;
5
use Sofa\Revisionable\Revisionable;
6
7
class Presenter
8
{
9
    /**
10
     * Revisionable fields labels.
11
     *
12
     * @var array
13
     */
14
    protected $labels = [];
15
16
    /**
17
     * Revision model.
18
     *
19
     * @var \Sofa\Revisionable\Laravel\Revision
20
     */
21
    protected $revision;
22
23
    /**
24
     * Revisoned model.
25
     *
26
     * @var \Illuminate\Database\Eloquent\Model
27
     */
28
    protected $revisioned;
29
30
    /**
31
     * Revisionable fields translated to real data. For example
32
     * show related model's property instead of its raw id.
33
     *
34
     * @var array
35
     */
36
    protected $passThrough = [];
37
38
    /**
39
     * Translate revision actions.
40
     *
41
     * @var array
42
     */
43
    protected $actions = [
44
        'created'  => 'created',
45
        'updated'  => 'updated',
46
        'deleted'  => 'deleted',
47
        'restored' => 'restored',
48
    ];
49
50
    /**
51
     * Old version of revisioned model.
52
     *
53
     * @var \Illuminate\Database\Eloquent\Model
54
     */
55
    protected $oldVersion;
56
57
    /**
58
     * New version of revisioned model.
59
     *
60
     * @var \Illuminate\Database\Eloquent\Model
61
     */
62
    protected $newVersion;
63
64
    /**
65
     * HTML templates.
66
     *
67
     * @var array
68
     */
69
    protected $templates = [];
70
71
    /**
72
     * Create a new revision presenter.
73
     *
74
     * @param \Sofa\Revisionable\Revision $revision
75
     * @param \Illuminat\Database\Eloquent\Model $revisioned
76
     */
77
    public function __construct(Revision $revision, Model $revisioned)
78
    {
79
        $this->revision   = $revision;
80
        $this->revisioned = $revisioned;
81
    }
82
83
    /**
84
     * Present action field.
85
     *
86
     * @return string
87
     */
88
    public function action()
89
    {
90
        $action = $this->revision->action;
0 ignored issues
show
The property action does not exist on object<Sofa\Revisionable\Laravel\Revision>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
91
92
        return array_get($this->actions, $action, $action);
93
    }
94
95
    /**
96
     * Get custom label for revisioned field.
97
     *
98
     * @param  string $key
99
     * @return string
100
     */
101
    public function label($key)
102
    {
103
        return array_get($this->labels, $key, $key);
104
    }
105
106
    /**
107
     * Get value from the revision.
108
     *
109
     * @param  string $version
110
     * @param  string $key
111
     * @return mixed
112
     */
113
    public function getFromRevision($version, $key)
114
    {
115
        return ($this->isPassedThrough($key))
116
            ? $this->passThrough($version, $key)
117
            : array_get($this->{$version}, $key);
118
    }
119
120
    /**
121
     * Determine whether the value should be fetched from the relation.
122
     *
123
     * @param  string $key
124
     * @return boolean
125
     */
126
    protected function isPassedThrough($key)
127
    {
128
        return array_key_exists($key, $this->passThrough);
129
    }
130
131
    /**
132
     * Get value from the relation.
133
     *
134
     * @param  string $version
135
     * @param  string $key
136
     * @return mixed
137
     */
138
    protected function passThrough($version, $key)
139
    {
140
        $revisioned = $this->getVersion($version);
141
142
        $needle = $this->passThrough[$key];
143
144
        return $this->dataGet($revisioned, $needle);
145
    }
146
147
    /**
148
     * Get pass through value using dot notation.
149
     *
150
     * @param  mixed  $target
151
     * @param  string $key
152
     * @return mixed
153
     */
154
    protected function dataGet($target, $key)
155
    {
156
        foreach (explode('.', $key) as $segment) {
157
            if ($target instanceof Revisionable) {
158
                $target = $this->passThroughRevisionable($target, $segment);
159
160
            } elseif ($target instanceof Presenter || $target instanceof Revision) {
161
                $target = $this->passThroughRevision($target, $segment);
0 ignored issues
show
It seems like $target defined by $this->passThroughRevision($target, $segment) on line 161 can also be of type object<Sofa\Revisionable\Laravel\Revision>; however, Sofa\Revisionable\Larave...::passThroughRevision() does only seem to accept object<Sofa\Revisionable...able\Laravel\Presenter>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
162
163
            } elseif ($target instanceof Model) {
164
                $target = $this->passThroughModel($target, $segment);
165
166
            } else {
167
                $target = null;
168
            }
169
170
            if (!$target) {
171
                return;
172
            }
173
        }
174
175
        return $target;
176
    }
177
178
    protected function passThroughRevisionable(Revisionable $revisionable, $key)
179
    {
180
        // Determine whether the model existed at the time of revision.
181
        if ($revisionable->created_at > $this->created_at) {
0 ignored issues
show
Accessing created_at on the interface Sofa\Revisionable\Revisionable suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
The property created_at does not exist on object<Sofa\Revisionable\Laravel\Presenter>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
182
            return;
183
        }
184
185
        $target = $revisionable->{$key};
186
187
        // If we are working with related revisionable model then
188
        // return its version at the time of current revision.
189
        if ($target instanceof Revisionable) {
190
            return ($target->revisionSnapshot($this->created_at)) ?: $target;
0 ignored issues
show
The property created_at does not exist on object<Sofa\Revisionable\Laravel\Presenter>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
The method revisionSnapshot() does not seem to exist on object<Sofa\Revisionable\Revisionable>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
191
        }
192
193
        return $target;
194
    }
195
196
    /**
197
     * Get pass through value from another revision.
198
     *
199
     * @param  \Sofa\Revisionable\Revision|\Sofa\Revisionable\Laravel\Presenter $revision
200
     * @param  string $key
201
     * @return mixed
202
     */
203
    protected function passThroughRevision($revision, $key)
204
    {
205
        $action = $revision->getAttribute('action');
0 ignored issues
show
Documentation Bug introduced by
The method getAttribute does not exist on object<Sofa\Revisionable\Laravel\Presenter>? 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...
206
207
        // @todo what about restored???
208
        if (in_array($action, ['created', 'updated'])) {
209
            return $revision->new($key);
0 ignored issues
show
Documentation Bug introduced by
The method new does not exist on object<Sofa\Revisionable\Laravel\Presenter>? 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...
210
        }
211
    }
212
213
    /**
214
     * Get pass through value from the Eloquent model.
215
     *
216
     * @param  \Illuminate\Database\Eloquent\Model $model
217
     * @param  string $key
218
     * @return mixed
219
     */
220
    protected function passThroughModel(Model $model, $key)
221
    {
222
        return $model->{$key};
223
    }
224
225
    /**
226
     * Get revisioned model with appropriate attributes.
227
     *
228
     * @return \Illuminate\Database\Eloquent\Model
229
     */
230
    protected function getVersion($version)
231
    {
232
        if (!$this->{$version.'Version'}) {
233
            $revisioned = get_class($this->revisioned);
234
235
            $revision = new $revisioned;
236
            $revision->setRawAttributes($this->{$version});
237
238
            $this->{$version.'Version'} = $revision;
239
        }
240
241
        return $this->{$version.'Version'};
242
    }
243
244
    /**
245
     * Decorate revision model or array/collection of models.
246
     *
247
     * @param  mixed $revision
248
     * @param  \Illuminate\Database\Eloquent\Model $revisioned
249
     * @return mixed
250
     *
251
     * @throws \InvalidArgumentException
252
     */
253
    public static function make($revision, $revisioned)
254
    {
255
        if (is_array($revision)) {
256
            return static::makeArray($revision, $revisioned);
257
        }
258
259
        if ($revision instanceof Collection) {
260
            return static::makeCollection($revision, $revisioned);
261
        }
262
263
        if (! $revision || $revision instanceof Model) {
264
            return static::makeOne($revision, $revisioned);
265
        }
266
267
        throw new \InvalidArgumentException(
268
            'Presenter::make accepts array, collection or single resource, '.gettype($revision).' given.'
269
        );
270
    }
271
272
    /**
273
     * Decorate Eloquent model.
274
     *
275
     * @param  \Illuminate\Database\Eloquent\Model|null $revision
276
     * @param  \Illuminate\Database\Eloquent\Model $revisioned
277
     * @return static
278
     */
279
    public static function makeOne(Model $revision, Model $revisioned)
280
    {
281
        return new static($revision, $revisioned);
0 ignored issues
show
$revision of type object<Illuminate\Database\Eloquent\Model> is not a sub-type of object<Sofa\Revisionable\Laravel\Revision>. It seems like you assume a child class of the class Illuminate\Database\Eloquent\Model 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...
282
    }
283
284
    /**
285
     * Decorate array of Eloquent models.
286
     *
287
     * @param  array $revisions
288
     * @param  \Illuminate\Database\Eloquent\Model $revisioned
289
     * @return array
290
     */
291
    public static function makeArray(array $revisions, Model $revisioned)
292
    {
293
        return array_map(static::getMapCallback($revisioned), $revisions);
294
    }
295
296
    /**
297
     * Decorate collection of models.
298
     *
299
     * @param  \Illuminate\Database\Eloquent\Collection $revisions
300
     * @param  \Illuminate\Database\Eloquent\Model $revisioned
301
     * @return \Illuminate\Database\Eloquent\Collection
302
     */
303
    public static function makeCollection(Collection $revisions, Model $revisioned)
304
    {
305
        return $revisions->map(static::getMapCallback($revisioned));
306
    }
307
308
    /**
309
     * Get callback for the array map.
310
     *
311
     * @return \Closure
312
     */
313
    protected static function getMapCallback($revisioned)
314
    {
315
        // We need to pass the calling class to the closure scope
316
        // instead of calling new static(), since php is going
317
        // to instantiate it w/o late static binding (bug).
318
        $presenter = get_called_class();
319
320
        return function ($revision) use ($presenter, $revisioned) {
321
            return new $presenter($revision, $revisioned);
322
        };
323
    }
324
325
    /**
326
     * Handle dynamic methods calls.
327
     *
328
     * @param  string $method
329
     * @param  array $parameters
330
     * @return mixed
331
     */
332
    public function __call($method, $parameters)
333
    {
334 View Code Duplication
        if (in_array($method, ['new', 'old'])) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
335
            array_unshift($parameters, $method);
336
337
            return call_user_func_array([$this, 'getFromRevision'], $parameters);
338
        }
339
340
        return call_user_func_array([$this->revision, $method], $parameters);
341
    }
342
343
    /**
344
     * Pass dynamic property calls on to underlying revision model.
345
     *
346
     * @param  string $property
347
     * @return mixed
348
     */
349
    public function __get($property)
350
    {
351
        // Return decorated property if method is defined on this presenter.
352
        if (method_exists($this, $property)) {
353
            return $this->$property();
354
        }
355
356
        return $this->revision->$property;
357
    }
358
}
359