BasePathTrait   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 38
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B getPath() 0 8 5
A basePath() 0 10 3
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
        if (strlen($basePath) > 1 && substr($basePath, -1) === '/') {
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