Issues (42)

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/Elements/Select.php (6 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\Html\Elements;
4
5
use Illuminate\Support\Str;
6
use Spatie\Html\Selectable;
7
use Spatie\Html\BaseElement;
8
use Illuminate\Support\Collection;
9
10
class Select extends BaseElement
11
{
12
    /** @var string */
13
    protected $tag = 'select';
14
15
    /** @var array */
16
    protected $options = [];
17
18
    /** @var string|iterable */
19
    protected $value = '';
20
21
    /**
22
     * @param bool $strict
23
     * @return static
24
     */
25
    public function multiple($strict = false)
26
    {
27
        $element = clone $this;
28
29
        $element = $element->attribute('multiple');
30
31
        $name = $element->getAttribute('name');
32
33
        if ($name && ! Str::endsWith($name, '[]')) {
34
            $element = $element->name($name.'[]');
35
        }
36
37
        $element->applyValueToOptions($strict);
38
39
        return $element;
40
    }
41
42
    /**
43
     * @param string|null $name
44
     *
45
     * @return static
46
     */
47
    public function name($name)
48
    {
49
        return $this->attribute('name', $name);
50
    }
51
52
    /**
53
     * @param iterable $options
54
     * @param bool $strict
55
     * @return static
56
     */
57 View Code Duplication
    public function options($options, $strict = false)
0 ignored issues
show
This method seems to be duplicated in 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...
58
    {
59
        return $this->addChildren($options, function ($text, $value) use ($strict) {
60
            if (is_array($text)) {
61
                return $this->optgroup($value, $text, $strict);
0 ignored issues
show
$text is of type array, but the function expects a object<Spatie\Html\Elements\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
62
            }
63
64
            return $this->createOption($text, $value, $strict);
65
        });
66
    }
67
68
    /**
69
     * @param string $label
70
     * @param iterable $options
71
     * @param bool $strict
72
     *
73
     * @return static
74
     */
75 View Code Duplication
    public function optgroup($label, $options, $strict = false)
0 ignored issues
show
This method seems to be duplicated in 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...
76
    {
77
        return Optgroup::create()
78
            ->label($label)
79
            ->addChildren($options, function ($text, $value) use ($strict) {
80
                return $this->createOption($text, $value, $strict);
81
            });
82
83
        return $this->addChild($optgroup);
0 ignored issues
show
return $this->addChild($optgroup); does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
84
    }
85
86
    /**
87
     * @param string|null $text
88
     *
89
     * @return static
90
     */
91
    public function placeholder($text)
92
    {
93
        return $this->prependChild(
94
            $this->createOption($text, null)
95
                ->selectedIf(! $this->hasSelection())
96
        );
97
    }
98
99
    /**
100
     * @return static
101
     */
102
    public function required()
103
    {
104
        return $this->attribute('required');
105
    }
106
107
    /**
108
     * @param string|iterable $value
109
     * @param bool $strict
110
     *
111
     * @return static
112
     */
113
    public function value($value = null, $strict = false)
114
    {
115
        $element = clone $this;
116
117
        $element->value = $value;
118
119
        $element->applyValueToOptions($strict);
120
121
        return $element;
122
    }
123
124
    protected function hasSelection()
125
    {
126
        return $this->children->contains->hasAttribute('selected');
127
    }
128
129
    protected function createOption($text, $value = null, $strict = false)
130
    {
131
        $selected = false;
132
        if (null !== $value) {
133
            $selected = $strict ?
134
                $value === $this->value :
135
                $value == $this->value;
136
        }
137
138
        return Option::create()
139
            ->value($value)
140
            ->text($text)
141
            ->selectedIf($selected);
142
    }
143
144
    protected function applyValueToOptions($strict = false)
145
    {
146
        $value = Collection::make($this->value);
147
148
        if (! $this->hasAttribute('multiple')) {
149
            $value = $value->take(1);
150
        }
151
152
        $this->children = $this->applyValueToElements($value, $this->children, $strict);
153
    }
154
155
    protected function applyValueToElements($value, Collection $children, $strict = false)
156
    {
157
        return $children->map(function ($child) use ($strict, $value) {
158
            if ($child instanceof Optgroup) {
159
                return $child->setChildren($this->applyValueToElements($value, $child->children, $strict));
0 ignored issues
show
The property children cannot be accessed from this context as it is declared protected in class Spatie\Html\BaseElement.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
$this->applyValueToEleme...ild->children, $strict) is of type object<Illuminate\Support\Collection>, but the function expects a array<integer,object<Spatie\Html\HtmlElement>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
160
            }
161
162
            if ($child instanceof Selectable) {
163
                $attribute = $child->getAttribute('value');
164
                $contains = $strict ?
165
                    $value->containsStrict($attribute) :
166
                    $value->contains($attribute);
167
168
                return $child->selectedIf($contains);
169
            }
170
171
            return $child;
172
        });
173
    }
174
}
175