Issues (10)

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/HasTranslations.php (8 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
namespace Spatie\Translatable;
4
5
use Illuminate\Support\Str;
6
use Spatie\Translatable\Events\TranslationHasBeenSet;
7
use Spatie\Translatable\Exceptions\AttributeIsNotTranslatable;
8
9
trait HasTranslations
10
{
11
    /**
12
     * @param string $key
13
     *
14
     * @return mixed
15
     */
16
    public function getAttributeValue($key)
17
    {
18
        if (!$this->isTranslatableAttribute($key)) {
19
            return parent::getAttributeValue($key);
20
        }
21
22
        return $this->getTranslation($key, config('app.locale'));
23
    }
24
25
    /**
26
     * @param string $key
27
     * @param string $locale
28
     *
29
     * @return mixed
30
     */
31
    public function translate($key, $locale = null)
32
    {
33
        return $this->getTranslation($key, $locale);
34
    }
35
36
    /***
37
     * @param string $key
38
     * @param string $locale
39
     *
40
     * @return mixed
41
     */
42
    public function getTranslation($key, $locale)
43
    {
44
        $locale = $this->normalizeLocale($key, $locale);
45
46
        $translations = $this->getTranslations($key);
47
48
        $translation = isset($translations[$locale]) ? $translations[$locale] : '';
49
50
        if ($this->hasGetMutator($key)) {
0 ignored issues
show
It seems like hasGetMutator() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
51
            return $this->mutateAttribute($key, $translation);
0 ignored issues
show
It seems like mutateAttribute() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
52
        }
53
54
        return $translation;
55
    }
56
57
    public function getTranslations($key)
58
    {
59
        $this->guardAgainstUntranslatableAttribute($key);
60
61
        return json_decode(isset($this->getAttributes()[$key]) ? $this->getAttributes()[$key] : '' ?: '{}', true);
0 ignored issues
show
It seems like getAttributes() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
62
    }
63
64
    /**
65
     * @param string $key
66
     * @param string $locale
67
     * @param $value
68
     *
69
     * @return $this
70
     */
71
    public function setTranslation($key, $locale, $value)
72
    {
73
        $this->guardAgainstUntranslatableAttribute($key);
74
75
        $translations = $this->getTranslations($key);
76
77
        $oldValue = isset($translations[$locale]) ? $translations[$locale] : '';
78
79
        if ($this->hasSetMutator($key)) {
0 ignored issues
show
It seems like hasSetMutator() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
80
            $method = 'set'.Str::studly($key).'Attribute';
81
            $value = $this->{$method}($value);
82
        }
83
84
        $translations[$locale] = $value;
85
86
        $this->attributes[$key] = $this->asJson($translations);
0 ignored issues
show
The property attributes 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...
It seems like asJson() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
87
88
        event(new TranslationHasBeenSet($this, $key, $locale, $oldValue, $value));
89
90
        return $this;
91
    }
92
93
    /**
94
     * @param string $key
95
     * @param array  $translations
96
     *
97
     * @return $this
98
     */
99
    public function setTranslations($key, array $translations)
100
    {
101
        $this->guardAgainstUntranslatableAttribute($key);
102
103
        foreach ($translations as $locale => $translation) {
104
            $this->setTranslation($key, $locale, $translation);
105
        }
106
107
        return $this;
108
    }
109
110
    /**
111
     * @param string $key
112
     * @param string $locale
113
     *
114
     * @return $this
115
     */
116
    public function forgetTranslation($key, $locale)
117
    {
118
        $translations = $this->getTranslations($key);
119
120
        unset($translations[$locale]);
121
122
        $this->setAttribute($key, $translations);
0 ignored issues
show
It seems like setAttribute() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
123
124
        return $this;
125
    }
126
127
    public function getTranslatedLocales($key)
128
    {
129
        return array_keys($this->getTranslations($key));
130
    }
131
132
    public function isTranslatableAttribute($key)
133
    {
134
        return in_array($key, $this->getTranslatableAttributes());
135
    }
136
137
    protected function guardAgainstUntranslatableAttribute($key)
138
    {
139
        if (!$this->isTranslatableAttribute($key)) {
140
            throw AttributeIsNotTranslatable::make($key, $this);
141
        }
142
    }
143
144
    protected function normalizeLocale($key, $locale)
145
    {
146
        if (in_array($locale, $this->getTranslatedLocales($key))) {
147
            return $locale;
148
        }
149
150
        if (!is_null($fallbackLocale = config('laravel-translatable.fallback_locale'))) {
151
            return $fallbackLocale;
152
        }
153
154
        return $locale;
155
    }
156
157
    public function getTranslatableAttributes()
158
    {
159
        return is_array($this->translatable)
0 ignored issues
show
The property translatable 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...
160
            ? $this->translatable
161
            : [];
162
    }
163
164
    public function getCasts()
165
    {
166
        return array_merge(
167
            parent::getCasts(),
168
            array_fill_keys($this->getTranslatableAttributes(), 'array')
169
        );
170
    }
171
}
172