Required::isValid()   B
last analyzed

Complexity

Conditions 6
Paths 8

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 11
rs 8.8571
cc 6
eloc 6
nc 8
nop 0
1
<?php
2
3
/**
4
 * @author: Abdul Qureshi. <[email protected]>
5
 * 
6
 * This file has been modified from the original source.
7
 * See original here:
8
 *
9
 * @link: https://github.com/progsmile/request-validator
10
 */
11
12
namespace TheSupportGroup\Common\Validator\Rules;
13
14
use TheSupportGroup\Common\Validator\Contracts\Rules\RuleInterface;
15
16
class Required extends BaseRule implements RuleInterface
17
{
18
    public function isValid()
0 ignored issues
show
Coding Style introduced by
isValid uses the super-global variable $_FILES which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
19
    {
20
        if ($this->isNotRequiredAndEmpty()) {
21
            return true;
22
        }
23
24
        $value = trim($this->getParams()[1]);
25
26
        return (bool) $value || $value == '0'
27
            || !empty($_FILES) && isset($_FILES[$this->getParams()[0]]) && $_FILES[$this->getParams()[0]]['name'];
28
    }
29
30
    public function getMessage()
31
    {
32
        return 'Field :field: is required';
33
    }
34
}
35