Issues (13)

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/HasStatuses.php (1 issue)

Check for unknown method on object

Bug Major

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
namespace Spatie\ModelStatus;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Relations\MorphMany;
7
use Illuminate\Database\Eloquent\Relations\Relation;
8
use Illuminate\Database\Query\Builder as QueryBuilder;
9
use Illuminate\Support\Arr;
10
use Illuminate\Support\Facades\DB;
11
use Spatie\ModelStatus\Events\StatusUpdated;
12
use Spatie\ModelStatus\Exceptions\InvalidStatus;
13
14
trait HasStatuses
15
{
16
    public function statuses(): MorphMany
17
    {
18
        return $this->morphMany($this->getStatusModelClassName(), 'model', 'model_type', $this->getModelKeyColumnName())
19
            ->latest('id');
20
    }
21
22
    public function status(): ?Status
23
    {
24
        return $this->latestStatus();
25
    }
26
27
    public function setStatus(string $name, ?string $reason = null): self
28
    {
29
        if (! $this->isValidStatus($name, $reason)) {
30
            throw InvalidStatus::create($name);
31
        }
32
33
        return $this->forceSetStatus($name, $reason);
34
    }
35
36
    public function isValidStatus(string $name, ?string $reason = null): bool
37
    {
38
        return true;
39
    }
40
41
    /**
42
     * @param string|array $names
43
     *
44
     * @return null|Status
45
     */
46
    public function latestStatus(...$names): ?Status
47
    {
48
        $statuses = $this->relationLoaded('statuses') ? $this->statuses : $this->statuses();
49
50
        $names = is_array($names) ? Arr::flatten($names) : func_get_args();
51
        if (count($names) < 1) {
52
            return $statuses->first();
53
        }
54
55
        return $statuses->whereIn('name', $names)->first();
56
    }
57
58
    public function hasEverHadStatus($name): bool
59
    {
60
        $statuses = $this->relationLoaded('statuses') ? $this->statuses : $this->statuses();
61
62
        return $statuses->where('name', $name)->count() > 0;
63
    }
64
65
    public function deleteStatus(...$names)
66
    {
67
        $names = is_array($names) ? Arr::flatten($names) : func_get_args();
68
        if (count($names) < 1) {
69
            return $this;
70
        }
71
72
        $this->statuses()->whereIn('name', $names)->delete();
0 ignored issues
show
The method whereIn() does not exist on Illuminate\Database\Eloquent\Relations\MorphMany. Did you maybe mean whereInMethod()?

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...
73
    }
74
75
    public function scopeCurrentStatus(Builder $builder, ...$names)
76
    {
77
        $names = is_array($names) ? Arr::flatten($names) : func_get_args();
78
        $builder
79
            ->whereHas(
80
                'statuses',
81
                function (Builder $query) use ($names) {
82
                    $query
83
                        ->whereIn('name', $names)
84
                        ->whereIn(
85
                            'id',
86
                            function (QueryBuilder $query) {
87
                                $query
88
                                    ->select(DB::raw('max(id)'))
89
                                    ->from($this->getStatusTableName())
90
                                    ->where('model_type', $this->getStatusModelType())
91
                                    ->whereColumn($this->getModelKeyColumnName(), $this->getQualifiedKeyName());
92
                            }
93
                        );
94
                }
95
            );
96
    }
97
98
    /**
99
     * @param string|array $names
100
     *
101
     * @return void
102
     **/
103
    public function scopeOtherCurrentStatus(Builder $builder, ...$names)
104
    {
105
        $names = is_array($names) ? Arr::flatten($names) : func_get_args();
106
        $builder
107
            ->whereHas(
108
                'statuses',
109
                function (Builder $query) use ($names) {
110
                    $query
111
                        ->whereNotIn('name', $names)
112
                        ->whereIn(
113
                            'id',
114
                            function (QueryBuilder $query) use ($names) {
115
                                $query
116
                                    ->select(DB::raw('max(id)'))
117
                                    ->from($this->getStatusTableName())
118
                                    ->where('model_type', $this->getStatusModelType())
119
                                    ->whereColumn($this->getModelKeyColumnName(), $this->getQualifiedKeyName());
120
                            }
121
                        );
122
                }
123
            )
124
            ->orWhereDoesntHave('statuses');
125
    }
126
127
    public function getStatusAttribute(): string
128
    {
129
        return (string) $this->latestStatus();
130
    }
131
132
    public function forceSetStatus(string $name, ?string $reason = null): self
133
    {
134
        $oldStatus = $this->latestStatus();
135
136
        $newStatus = $this->statuses()->create([
137
            'name'   => $name,
138
            'reason' => $reason,
139
        ]);
140
141
        event(new StatusUpdated($oldStatus, $newStatus, $this));
142
143
        return $this;
144
    }
145
146
    protected function getStatusTableName(): string
147
    {
148
        $modelClass = $this->getStatusModelClassName();
149
150
        return (new $modelClass)->getTable();
151
    }
152
153
    protected function getModelKeyColumnName(): string
154
    {
155
        return config('model-status.model_primary_key_attribute') ?? 'model_id';
156
    }
157
158
    protected function getStatusModelClassName(): string
159
    {
160
        return config('model-status.status_model');
161
    }
162
163
    protected function getStatusModelType(): string
164
    {
165
        return array_search(static::class, Relation::morphMap()) ?: static::class;
166
    }
167
}
168