SilexParameterReader   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 54
Duplicated Lines 35.19 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 19
loc 54
c 0
b 0
f 0
wmc 9
lcom 1
cbo 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A read() 0 6 2
A has() 0 4 2
A map() 19 19 4

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
namespace rtens\domin\delivery\web\adapters\silex;
3
4
use rtens\domin\delivery\ParameterReader;
5
use rtens\domin\Parameter;
6
use Symfony\Component\HttpFoundation\File\UploadedFile;
7
use Symfony\Component\HttpFoundation\Request;
8
9
class SilexParameterReader implements ParameterReader {
10
11
    /** @var Request */
12
    private $request;
13
14
    /**
15
     * @param Request $request
16
     */
17
    public function __construct(Request $request) {
18
        $this->request = $request;
19
    }
20
21
    /**
22
     * IMPORTANT: files must be properly merged into the parameters
23
     *
24
     * @param Parameter $parameter
25
     * @return mixed The serialized paramater
26
     */
27
    public function read(Parameter $parameter) {
28
        if ($this->request->files->has($parameter->getName())) {
29
            return $this->map($this->request->files->get($parameter->getName()));
30
        }
31
        return $this->map($this->request->get($parameter->getName()));
32
    }
33
34
    /**
35
     * @param Parameter $parameter
36
     * @return boolean
37
     */
38
    public function has(Parameter $parameter) {
39
        return $this->request->files->has($parameter->getName())
40
        || $this->request->get($parameter->getName(), '__NOPEDINOPE__') != '__NOPEDINOPE__';
41
    }
42
43 View Code Duplication
    private function map($value) {
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...
44
        if (is_array($value)) {
45
            $mapped = [];
46
            foreach ($value as $key => $item) {
47
                $mapped[$key] = $this->map($item);
48
            }
49
            return $mapped;
50
        } else if ($value instanceof UploadedFile) {
0 ignored issues
show
Bug introduced by
The class Symfony\Component\HttpFoundation\File\UploadedFile does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
51
            return [
52
                'name' => $value->getClientOriginalName(),
53
                'tmp_name' => $value->getPath() . DIRECTORY_SEPARATOR . $value->getFilename(),
54
                'error' => $value->getError(),
55
                'size' => $value->getSize(),
56
                'type' => $value->getType()
57
            ];
58
        } else {
59
            return $value;
60
        }
61
    }
62
}