Completed
Push — master ( 1b2d0a...89ea1a )
by Oscar
03:00
created

BasePathTrait::getPath()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 8.8571
cc 5
eloc 4
nc 6
nop 1
1
<?php
2
3
namespace Psr7Middlewares\Utils;
4
5
/**
6
 * Utilities used by middlewares with basePath options.
7
 */
8
trait BasePathTrait
9
{
10
    private $basePath = '';
11
12
    /**
13
     * Set the basepath used in the request.
14
     *
15
     * @param string $basePath
16
     *
17
     * @return self
18
     */
19
    public function basePath($basePath)
20
    {
21 View Code Duplication
        if (strlen($basePath) > 1 && substr($basePath, -1) === '/') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
22
            $this->basePath = substr($basePath, 0, -1);
23
        } else {
24
            $this->basePath = $basePath;
25
        }
26
27
        return $this;
28
    }
29
30
    /**
31
     * Removes the basepath from a path.
32
     *
33
     * @param string $path
34
     *
35
     * @return string
36
     */
37
    private function getPath($path)
38
    {
39
        if ($this->basePath === '' || strpos($path, $this->basePath) === 0) {
40
            $path = substr($path, strlen($this->basePath)) ?: '';
41
        }
42
43
        return $path === '' ? '/' : $path;
44
    }
45
}
46