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

BasePathTrait   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 38
Duplicated Lines 13.16 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A basePath() 5 10 3
B getPath() 0 8 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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