Issues (3)

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

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
declare(strict_types=1);
4
5
namespace Arcanedev\LaravelSitemap\Entities;
6
7
use Arcanedev\LaravelSitemap\Contracts\Entities\Url as UrlContract;
8
use Arcanedev\LaravelSitemap\Exceptions\SitemapException;
9
use DateTime;
10
use DateTimeInterface;
11
use Illuminate\Support\{Arr, Fluent};
12
13
/**
14
 * Class     Url
15
 *
16
 * @package  Arcanedev\LaravelSitemap\Entities
17
 * @author   ARCANEDEV <[email protected]>
18
 */
19
class Url extends Fluent implements UrlContract
20
{
21
    /* -----------------------------------------------------------------
22
     |  Constructor
23
     | -----------------------------------------------------------------
24
     */
25
26
    /**
27
     * Url constructor.
28
     *
29
     * @param  array|string  $attributes
30
     */
31 246
    public function __construct($attributes = [])
32
    {
33 246
        if (is_string($attributes))
34 102
            $attributes = ['loc' => $attributes];
35
36 246
        parent::__construct($attributes);
37
38 246
        $this->setLoc(Arr::get($attributes, 'loc'));
39 246
        $this->setLastMod(Arr::get($attributes, 'lastmod', new DateTime));
40 246
        $this->setChangeFreq(Arr::get($attributes, 'changefreq', ChangeFrequency::DAILY));
41 246
        $this->setPriority(Arr::get($attributes, 'priority', 0.8));
42 246
        $this->setTitle(Arr::get($attributes, 'title'));
43 246
    }
44
45
    /* -----------------------------------------------------------------
46
     |  Getters & Setters
47
     | -----------------------------------------------------------------
48
     */
49
50
    /**
51
     * Get the url location.
52
     *
53
     * @return string
54
     */
55 162
    public function getLoc(): string
56
    {
57 162
        return $this->escape($this->get('loc'));
58
    }
59
60
    /**
61
     * Set the url location.
62
     *
63
     * @param  string  $loc
64
     *
65
     * @return $this
66
     */
67 246
    public function setLoc($loc)
68
    {
69 246
        return $this->set('loc', $this->checkLoc($loc));
70
    }
71
72
    /**
73
     * Get the last modification date.
74
     *
75
     * @return \DateTimeInterface
76
     */
77 120
    public function getLastMod(): DateTimeInterface
78
    {
79 120
        return $this->get('lastmod');
80
    }
81
82
    /**
83
     * Format the url last modification.
84
     *
85
     * @param  string  $format
86
     *
87
     * @return string
88
     */
89 102
    public function formatLastMod(string $format = DateTimeInterface::ATOM): string
90
    {
91 102
        return $this->getLastMod()->format($format);
92
    }
93
94
    /**
95
     * Set the last modification date.
96
     *
97
     * @param  string|\DateTimeInterface  $lastModDate
98
     * @param  string                     $format
99
     *
100
     * @return $this
101
     */
102 246
    public function setLastMod($lastModDate, string $format = 'Y-m-d H:i:s')
103
    {
104 246
        if (is_string($lastModDate))
105 156
            $lastModDate = DateTime::createFromFormat($format, $lastModDate);
106
107 246
        return $this->set('lastmod', $lastModDate);
108
    }
109
110
    /**
111
     * Get the change frequency.
112
     *
113
     * @return string
114
     */
115 24
    public function getChangeFreq(): string
116
    {
117 24
        return $this->get('changefreq');
118
    }
119
120
    /**
121
     * Set the change frequency.
122
     *
123
     * @param  string  $changeFreq
124
     *
125
     * @return $this
126
     */
127 246
    public function setChangeFreq(string $changeFreq)
128
    {
129 246
        return $this->set('changefreq', strtolower(trim($changeFreq)));
130
    }
131
132
    /**
133
     * Get the priority.
134
     *
135
     * @return float
136
     */
137 24
    public function getPriority(): float
138
    {
139 24
        return $this->get('priority');
140
    }
141
142
    /**
143
     * Set the priority.
144
     *
145
     * @param  float|mixed  $priority
146
     *
147
     * @return $this
148
     */
149 246
    public function setPriority($priority)
150
    {
151 246
        $priority = $this->checkPriority($priority);
152
153 246
        return $this->set('priority', $priority);
154
    }
155
156
    /**
157
     * Get the title.
158
     *
159
     * @return string|null
160
     */
161 30
    public function getTitle(): ?string
162
    {
163 30
        return $this->escape($this->get('title'));
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->escape($this->get('title')); (null|object|integer|double|string|array|boolean) is incompatible with the return type declared by the interface Arcanedev\LaravelSitemap...\Entities\Url::getTitle of type string|null.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
164
    }
165
166
    /**
167
     * Get the title.
168
     *
169
     * @param  string|null  $title
170
     *
171
     * @return $this
172
     */
173 246
    public function setTitle(?string $title)
174
    {
175 246
        return $this->set('title', $title);
176
    }
177
178
    /**
179
     * Set an attribute.
180
     *
181
     * @param  string  $key
182
     * @param  mixed   $value
183
     *
184
     * @return $this
185
     */
186 246
    public function set(string $key, $value)
187
    {
188 246
        $this->attributes[$key] = $value;
189
190 246
        return $this;
191
    }
192
193
    /* -----------------------------------------------------------------
194
     |  Main Methods
195
     | -----------------------------------------------------------------
196
     */
197
198
    /**
199
     * Create a sitemap url instance.
200
     *
201
     * @param  string  $loc
202
     *
203
     * @return $this
204
     */
205 180
    public static function make($loc)
206
    {
207 180
        return new static(compact('loc'));
208
    }
209
210
    /**
211
     * Make a URL instance with attributes.
212
     *
213
     * @param  array  $attributes
214
     *
215
     * @return $this
216
     */
217 24
    public static function makeFromArray(array $attributes)
218
    {
219 24
        return new static($attributes);
220
    }
221
222
    /**
223
     * Check if has an attribute.
224
     *
225
     * @param  string  $key
226
     *
227
     * @return bool
228
     */
229 42
    public function has(string $key): bool
230
    {
231 42
        return ! is_null($this->get($key));
232
    }
233
234
    /**
235
     * Convert the Fluent instance to an array.
236
     *
237
     * @return array
238
     */
239 66
    public function toArray()
240
    {
241 66
        return array_merge(parent::toArray(), [
242 66
            'lastmod' => $this->formatLastMod(),
243
        ]);
244
    }
245
246
    /* -----------------------------------------------------------------
247
     |  Other Methods
248
     | -----------------------------------------------------------------
249
     */
250
251
    /**
252
     * Escape the given value.
253
     *
254
     * @param  string|mixed  $value
255
     *
256
     * @return string|null
257
     */
258 168
    protected function escape($value)
259
    {
260 168
        if (is_null($value))
261 6
            return $value;
262
263 168
        if (config('sitemap.escaping', true))
264 168
            $value = htmlentities($value, ENT_XML1, 'UTF-8');
265
266 168
        return $value;
267
    }
268
269
    /**
270
     * Check the loc value.
271
     *
272
     * @param  string  $loc
273
     *
274
     * @return string
275
     *
276
     * @throws \Arcanedev\LaravelSitemap\Exceptions\SitemapException
277
     */
278 246
    protected function checkLoc($loc): string
279
    {
280 246
        if ( ! is_string($loc))
281 12
            throw new SitemapException('The [loc] attribute is required and must be string value.');
282
283 246
        return $loc;
284
    }
285
286
    /**
287
     * Check the priority value.
288
     *
289
     * @param  float|mixed  $priority
290
     *
291
     * @return float
292
     *
293
     * @throws \Arcanedev\LaravelSitemap\Exceptions\SitemapException
294
     */
295 246
    protected function checkPriority($priority): float
296
    {
297 246
        if ( ! is_numeric($priority))
298 6
            throw new SitemapException("The [priority] value must be numeric.");
299
300 246
        $priority = round($priority, 1);
301
302 246
        if ($priority > 1 || $priority < 0)
303 6
            throw new SitemapException("The [priority] value must be between `0.0` and `1.0`, `{$priority}` was given.");
304
305 246
        return $priority;
306
    }
307
}
308