Completed
Push — master ( 579af5...b29473 )
by Oleg
07:53
created

FileValidator::validate()   C

Complexity

Conditions 12
Paths 8

Size

Total Lines 34
Code Lines 19

Duplication

Lines 17
Ratio 50 %
Metric Value
dl 17
loc 34
rs 5.1612
cc 12
eloc 19
nc 8
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php /** MicroFileValidator */
2
3
namespace Micro\Validator;
4
5
use Micro\Form\IFormModel;
6
7
/**
8
 * EmailValidator class file.
9
 *
10
 * @author Oleg Lunegov <[email protected]>
11
 * @link https://github.com/lugnsk/micro
12
 * @copyright Copyright &copy; 2013 Oleg Lunegov
13
 * @license /LICENSE
14
 * @package Micro
15
 * @subpackage Validator
16
 * @version 1.0
17
 * @since 1.0
18
 */
19
class FileValidator extends BaseValidator
20
{
21
    /**
22
     * @inheritdoc
23
     */
24
    public function validate(IFormModel $model)
25
    {
26
        foreach ($this->elements AS $element) {
27
            if (!$model->checkAttributeExists($element)) {
28
                $files = $this->container->request->getFiles();
0 ignored issues
show
Bug introduced by
Accessing request on the interface Micro\Base\IContainer suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
29
                if (!empty($this->params['maxFiles']) && (count($files->files) > $this->params['maxFiles'])) {
30
                    $this->errors[] = 'Too many files in parameter ' . $element;
31
32
                    return false;
33
                }
34
                foreach ($files->files AS $fContext) {
35 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...
36
                                $fContext['type']) === false)
37
                    ) {
38
                        $this->errors[] = 'File ' . $fContext['name'] . ' not allowed type';
39
40
                        return false;
41
                    }
42 View Code Duplication
                    if (!empty($this->params['minSize']) && ($fContext['size'] < $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...
43
                        $this->errors[] = 'File ' . $fContext['name'] . ' too small size';
44
45
                        return false;
46
                    }
47 View Code Duplication
                    if (!empty($this->params['maxSize']) && ($fContext['type'] > $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...
48
                        $this->errors[] = 'File ' . $fContext['name'] . ' too many size';
49
50
                        return false;
51
                    }
52
                }
53
            }
54
        }
55
56
        return true;
57
    }
58
59
    /**
60
     * @inheritdoc
61
     */
62
    public function client(IFormModel $model)
63
    {
64
        return '';
65
    }
66
}
67