Passed
Push — master ( 6248da...8ce640 )
by László
02:40
created

MonolithHandler   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 53
Duplicated Lines 18.87 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 86.67%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 5
dl 10
loc 53
ccs 13
cts 15
cp 0.8667
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handle() 10 10 2
A save() 0 14 1

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 CodingSocks\UploadHandler\Driver;
4
5
use Closure;
6
use CodingSocks\UploadHandler\Response\PercentageJsonResponse;
7
use CodingSocks\UploadHandler\StorageConfig;
8
use Illuminate\Http\Request;
9
use Symfony\Component\HttpFoundation\Response;
10
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
11
12
class MonolithHandler extends BaseHandler
13
{
14
    /**
15
     * @var string
16
     */
17
    private $fileParam;
18
19
    /**
20
     * MonolithDriver constructor.
21
     *
22
     * @param array $config
23
     */
24 7
    public function __construct($config)
25
    {
26 7
        $this->fileParam = $config['param'];
27 7
    }
28
29
    /**
30
     * {@inheritDoc}
31
     */
32 4 View Code Duplication
    public function handle(Request $request, StorageConfig $config, Closure $fileUploaded = null): Response
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
33
    {
34 4
        if ($request->isMethod(Request::METHOD_POST)) {
35 4
            return $this->save($request, $config, $fileUploaded);
36
        }
37
38
        throw new MethodNotAllowedHttpException([
39
            Request::METHOD_POST,
40
        ]);
41
    }
42
43
    /**
44
     * @param \Illuminate\Http\Request $request
45
     * @param \CodingSocks\UploadHandler\StorageConfig $config
46
     * @param \Closure|null $fileUploaded
47
     *
48
     * @return \Symfony\Component\HttpFoundation\Response
49
     */
50 4
    public function save(Request $request, StorageConfig $config, Closure $fileUploaded = null): Response
51
    {
52 4
        $file = $request->file($this->fileParam);
53
54 4
        $this->validateUploadedFile($file);
0 ignored issues
show
Bug introduced by
It seems like $file defined by $request->file($this->fileParam) on line 52 can also be of type array; however, CodingSocks\UploadHandle...:validateUploadedFile() does only seem to accept null|object<Illuminate\Http\UploadedFile>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
55
56 2
        $path = $file->store($config->getMergedDirectory(), [
57 2
            'disk' => $config->getDisk(),
58
        ]);
59
60 2
        $this->triggerFileUploadedEvent($config->getDisk(), $path, $fileUploaded);
61
62 2
        return new PercentageJsonResponse(100);
63
    }
64
}
65