Issues (8)

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/Version.php (7 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
 * CalamandreiLorenzo\LaravelVersionable
4
 *
5
 * This file is part of the overtrue/laravel-versionable.
6
 * ------------------------------------------------------
7
 * (c) overtrue <[email protected]>
8
 * This source file is subject to the MIT license that is bundled.
9
 */
10
11
namespace CalamandreiLorenzo\LaravelVersionable;
12
13
use CalamandreiLorenzo\LaravelVersionable\Exceptions\NotVersionableModel;
14
use Illuminate\Database\Eloquent\Model;
15
use Illuminate\Database\Eloquent\Relations\BelongsTo;
16
use Illuminate\Database\Eloquent\Relations\MorphTo;
17
use Illuminate\Database\Eloquent\SoftDeletes;
18
use Illuminate\Support\Arr;
19
use Illuminate\Support\Str;
20
use SebastianBergmann\Diff\Differ;
21
use function array_keys;
22
use function auth;
23
use function class_uses;
24
use function config;
25
use function in_array;
26
27
/**
28
 * Class Version.
29
 *
30
 * @property $id
31
 * @property Model $versionable
32
 * @property array $contents
33
 * @property int $version_number
34
 * @property $versionable_id
35
 * @property $versionable_type
36
 */
37
class Version extends Model
38
{
39
    use SoftDeletes;
40
41
    /**
42
     * @var string[] $casts
43
     */
44
    protected $casts = [
45
        'contents' => 'array',
46
    ];
47
48 8
    public function __construct(array $attributes = [])
49
    {
50
        if (
51 8
            config('versionable.key_type', VersionPrimaryKey::UUID)
52 8
            === VersionPrimaryKey::UUID
53
        ) {
54 8
            $this->keyType = "string";
55
        }
56 8
        parent::__construct($attributes);
57 8
    }
58
59
    /**
60
     * boot
61
     */
62 8
    protected static function boot(): void
63
    {
64 8
        parent::boot();
65
66
        self::creating(static function (Version $version) {
67
            if (
68 8
                config('versionable.key_type', VersionPrimaryKey::UUID)
69 8
                    === VersionPrimaryKey::UUID
70
            ) {
71 8
                $version->id = Str::uuid();
72
            }
73 8
        });
74 8
    }
75
76
    /**
77
     * @return BelongsTo
78
     */
79
    public function user(): BelongsTo
80
    {
81
        return $this->belongsTo(
82
            config('versionable.user_model'),
83
            config('versionable.user_foreign_key')
84
        );
85
    }
86
87
    /**
88
     * @return MorphTo
89
     */
90 2
    public function versionable(): MorphTo
91
    {
92 2
        return $this->morphTo('versionable');
93
    }
94
95
    /**
96
     * createForModel
97
     * @param Versionable|Model $model
0 ignored issues
show
Consider making the type for parameter $model a bit more specific; maybe use Model.
Loading history...
98
     * @return Version
99
     * @throws NotVersionableModel
100
     */
101 8
    public static function createForModel(Model $model): Version
102
    {
103 8 View Code Duplication
        if (!in_array(Versionable::class, class_uses($model), true)) {
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...
104
            throw new NotVersionableModel(
105
                "{$model} not use Versionable " . config('versionable.version_model')
106
            );
107
        }
108 8
        $versionClass = $model->getVersionModel();
109
        /** @var Version $version */
110 8
        $version = new $versionClass();
111 8
        $version->version_number = ((int) $versionClass::where('versionable_id', $model->getKey())
112 8
            ->where('versionable_type', $model->getMorphClass())
113 8
            ->max('version_number')) + 1;
114 8
        $version->versionable_id = $model->getKey();
115 8
        $version->versionable_type = $model->getMorphClass();
116 8
        $version->{config('versionable.user_foreign_key')} = auth()->id();
0 ignored issues
show
The method id does only exist in Illuminate\Contracts\Auth\Guard, but not in Illuminate\Contracts\Auth\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
117 8
        $version->contents = $model->getVersionableAttributes();
118
119 8
        $version->save();
120 8
        return $version;
121
    }
122
123
    /**
124
     * @return bool
125
     */
126 1
    public function revert(): bool
127
    {
128 1
        return $this->versionable->fill($this->contents)->save();
129
    }
130
131
    /**
132
     * diff
133
     * @param Versionable|Model|null $model
0 ignored issues
show
Consider making the type for parameter $model a bit more specific; maybe use null|Model.
Loading history...
134
     *
135
     * @return string
136
     * @throws NotVersionableModel
137
     */
138 1
    public function diff(Model $model = null): string
139
    {
140 1
        $model || $model = $this->versionable;
0 ignored issues
show
Consider using a different name than the parameter $model. This often makes code more readable.
Loading history...
141
142 1
        if ($model instanceof self) {
143 1
            $source = $model->contents;
144
        } else {
145 1 View Code Duplication
            if (!in_array(Versionable::class, class_uses($model), true)) {
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...
146
                throw new NotVersionableModel("{$model} is not versionable");
147
            }
148
149 1
            $source = $model->versionableFromArray($this->versionable->toArray());
0 ignored issues
show
The method versionableFromArray cannot be called on $model (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
150
        }
151
152 1
        return (new Differ())->diff(Arr::only($source, array_keys($this->contents)), $this->contents);
153
    }
154
}
155