FileValidator   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 49
Duplicated Lines 34.69 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
c 1
b 0
f 0
lcom 1
cbo 5
dl 17
loc 49
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
C validate() 17 35 12
A client() 0 4 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 /** MicroFileValidator */
2
3
namespace Micro\Validator;
4
5
use Micro\Form\IFormModel;
6
use Micro\Web\RequestInjector;
7
use Psr\Http\Message\UploadedFileInterface;
8
9
/**
10
 * EmailValidator class file.
11
 *
12
 * @author Oleg Lunegov <[email protected]>
13
 * @link https://github.com/linpax/microphp-framework
14
 * @copyright Copyright (c) 2013 Oleg Lunegov
15
 * @license https://github.com/linpax/microphp-framework/blob/master/LICENSE
16
 * @package Micro
17
 * @subpackage Validator
18
 * @version 1.0
19
 * @since 1.0
20
 */
21
class FileValidator extends BaseValidator
22
{
23
    /**
24
     * @inheritdoc
25
     */
26
    public function validate(IFormModel $model)
27
    {
28
        foreach ($this->elements AS $element) {
29
            if (!$model->checkAttributeExists($element)) {
30
                /** @var UploadedFileInterface[] $files */
31
                $files = (new RequestInjector)->build()->getUploadedFiles();
32
                if (!empty($this->params['maxFiles']) && (count($files) > $this->params['maxFiles'])) {
33
                    $this->errors[] = 'Too many files in parameter '.$element;
34
35
                    return false;
36
                }
37
                foreach ($files AS $fContext) {
38 View Code Duplication
                    if (!empty($this->params['types']) && (strpos($this->params['types'],
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...
39
                                $fContext->getClientMediaType()) === false)
40
                    ) {
41
                        $this->errors[] = 'File '.$fContext->getClientFilename().' not allowed type';
42
43
                        return false;
44
                    }
45 View Code Duplication
                    if (!empty($this->params['minSize']) && ($fContext->getSize() < $this->params['minSize'])) {
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...
46
                        $this->errors[] = 'File '.$fContext->getClientFilename().' too small size';
47
48
                        return false;
49
                    }
50 View Code Duplication
                    if (!empty($this->params['maxSize']) && ($fContext->getSize() > $this->params['maxSize'])) {
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...
51
                        $this->errors[] = 'File '.$fContext->getClientFilename().' too many size';
52
53
                        return false;
54
                    }
55
                }
56
            }
57
        }
58
59
        return true;
60
    }
61
62
    /**
63
     * @inheritdoc
64
     */
65
    public function client(IFormModel $model)
66
    {
67
        return '';
68
    }
69
}
70