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/Scope.php (4 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 Thinktomorrow\Locale;
4
5
use Thinktomorrow\Locale\Exceptions\InvalidScope;
6
use Thinktomorrow\Locale\Values\Locale;
7
use Thinktomorrow\Locale\Values\Root;
8
9
class Scope
10
{
11
    /**
12
     * @var array
13
     */
14
    private $locales;
15
16
    /**
17
     * The active locale.
18
     *
19
     * @var Locale
20
     */
21
    private static $activeLocale;
22
23
    /**
24
     * The default locale. In the request path
25
     * it's the hidden segment, e.g. /.
26
     *
27
     * @var Locale
28
     */
29
    private $default;
30
31
    /**
32
     * When the canonical scope has a root set to be
33
     * other than the current, that specific root is defined here
34
     * By default the current request root is of use (NULL).
35
     *
36
     * @var null|Root
37
     */
38
    private $customRoot = null;
39
40 86
    public function __construct(array $locales)
41
    {
42 86
        if (!isset($locales['/'])) {
43 1
            throw new InvalidScope('Default locale is required for scope. Add this as \'/\' => locale.');
44
        }
45 86
        $this->locales = $locales;
46 86
        $this->default = Locale::from($this->locales['/']);
47 86
    }
48
49 12
    public function setCustomRoot(Root $customRoot)
50
    {
51 12
        $this->customRoot = $customRoot;
52
53 12
        return $this;
54
    }
55
56 10
    public function customRoot(): ?Root
57
    {
58 10
        return $this->customRoot;
59
    }
60
61
    /**
62
     * Get the locale by segment identifier.
63
     *
64
     * @param $segment
65
     *
66
     * @return null|Locale
67
     */
68 72
    public function findLocale($segment): ?Locale
69
    {
70 72
        return isset($this->locales[$segment]) ? Locale::from($this->locales[$segment]) : null;
71
    }
72
73 35
    public function locales(): array
74
    {
75 35
        return $this->locales;
76
    }
77
78 65
    public function defaultLocale(): Locale
79
    {
80 65
        return $this->default;
81
    }
82
83 18
    public static function activeLocale(): ?Locale
84
    {
85 18
        return static::$activeLocale;
0 ignored issues
show
Since $activeLocale is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $activeLocale to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
86
    }
87
88 72
    public static function setActiveLocale(Locale $locale)
89
    {
90 72
        static::$activeLocale = $locale;
0 ignored issues
show
Since $activeLocale is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $activeLocale to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
91 72
    }
92
93
    /**
94
     * Get the url segment which corresponds with the passed locale.
95
     *
96
     * @param null $locale
97
     *
98
     * @return null|string
99
     */
100 39
    public function segment($locale = null): ?string
101
    {
102 39
        if (is_null($locale)) {
103 2
            return $this->activeSegment();
104
        }
105
106 39
        return ($key = array_search($locale, $this->locales)) ? $key : null;
107
    }
108
109 18
    public function activeSegment(): ?string
110
    {
111 18
        return $this->segment($this->activeLocale());
112
    }
113
114 30
    public function validateLocale(string $locale = null): bool
115
    {
116 30
        if (!$locale) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $locale of type null|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
117 5
            return false;
118
        }
119
120 28
        return in_array($locale, $this->locales);
121
    }
122
123 27
    public function validateSegment(string $segment = null): bool
124
    {
125 27
        if (!$segment) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $segment of type null|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
126 5
            return false;
127
        }
128
129 25
        return isset($this->locales[$segment]);
130
    }
131
}
132