Completed
Push — master ( 206c33...9e77c2 )
by Oscar
05:44
created

FileTrait   C

Complexity

Total Complexity 78

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 3
Bugs 2 Features 0
Metric Value
wmc 78
c 3
b 2
f 0
lcom 1
cbo 4
dl 0
loc 35
rs 5.4564

1 Method

Rating   Name   Duplication   Size   Complexity  
B getFilename() 0 22 6

How to fix   Complexity   

Complex Class

Complex classes like FileTrait often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use FileTrait, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Psr7Middlewares\Utils;
4
5
use Psr\Http\Message\RequestInterface;
6
7
/**
8
 * Common methods used by middlewares that read/write files.
9
 */
10
trait FileTrait
11
{
12
    use BasePathTrait;
13
    use StorageTrait;
14
15
    /**
16
     * Returns the filename of the response file.
17
     *
18
     * @param RequestInterface $request
19
     *
20
     * @return string
21
     */
22
    protected function getFilename(RequestInterface $request)
23
    {
24
        $path = $this->getBasePath($request->getUri()->getPath());
25
26
        $parts = pathinfo($path);
27
        $path = '/'.(isset($parts['dirname']) ? $parts['dirname'] : '');
28
        $filename = isset($parts['basename']) ? $parts['basename'] : '';
29
30
        //if it's a directory, append "/index.html"
31
        if (empty($parts['extension'])) {
32
            if ($path === '/') {
33
                $path .= $filename;
34
            } else {
35
                $path .= "/{$filename}";
36
            }
37
38
            $extension = strtolower(pathinfo($request->getUri()->getPath(), PATHINFO_EXTENSION)) ?: 'html';
39
            $filename = "index.{$extension}";
40
        }
41
42
        return "{$this->storage}{$path}/{$filename}";
43
    }
44
}
45