Validate   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 16
lcom 1
cbo 3
dl 0
loc 137
c 1
b 0
f 0
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 2
B loadRules() 0 29 6
A val() 0 12 2
A hasErrors() 0 8 2
A getErrorMessages() 0 4 1
A getCustomRules() 0 4 1
A setRegex() 0 5 1
A getRegex() 0 4 1
1
<?php
2
/**
3
 * Jaeger
4
 *
5
 * @copyright	Copyright (c) 2015-2016, mithra62
6
 * @link		http://jaeger-app.com
7
 * @version		1.0
8
 * @filesource 	./Validate.php
9
 */
10
namespace JaegerApp;
11
12
use Valitron\Validator;
13
use JaegerApp\Exceptions\ValidateException;
14
15
/**
16
 * mithra62 - Validation Object
17
 *
18
 * Object to manage system settings
19
 *
20
 * @package Validate
21
 * @author Eric Lamb <[email protected]>
22
 */
23
class Validate extends Validator
24
{
25
26
    /**
27
     * The regex engine
28
     * 
29
     * @var \mithra62\Regex
30
     */
31
    protected $regex = null;
32
33
    /**
34
     * Overrides the initial validation constructor
35
     * 
36
     * @throws \InvalidArgumentException
37
     */
38
    public function __construct()
39
    {
40
        $langDir = static::langDir();
41
        
42
        $lang = static::lang();
43
        // Load language file in directory
44
        $langFile = rtrim($langDir, '/') . '/' . $lang . '.php';
45
        if (stream_resolve_include_path($langFile)) {
46
            $langMessages = include $langFile;
47
            static::$_ruleMessages = array_merge(static::$_ruleMessages, $langMessages);
48
        } else {
49
            throw new \InvalidArgumentException("fail to load language file '$langFile'");
50
        }
51
        
52
        $this->loadRules();
53
    }
54
55
    /**
56
     * Adds all the custom rules
57
     * 
58
     * @throws ValidateException
59
     */
60
    protected function loadRules()
61
    {
62
        $old_cwd = getcwd();
63
        chdir(__DIR__);
64
        $path = 'Validate' . DIRECTORY_SEPARATOR . 'Rules';
65
        if (! is_dir($path)) {
66
            throw new ValidateException("Rules Directory " . $path . " isn't a directory...");
67
        }
68
        
69
        $files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path), \RecursiveIteratorIterator::LEAVES_ONLY);
70
        
71
        foreach ($files as $file) {
72
            if (! $file->isDir()) {
73
                $name = '\\JaegerApp\\' . str_replace('/', '\\', str_replace('.php', '', $file->getPathname()));
74
                $class = $name;
75
                if (class_exists($class)) {
76
                    $rule = new $class();
77
                    if ($rule instanceof Validate\RuleInterface) {
78
                        self::addRule($rule->getName(), array(
79
                            $rule,
80
                            'validate'
81
                        ), $rule->getErrorMessage());
0 ignored issues
show
Documentation introduced by
$rule->getErrorMessage() is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
82
                    }
83
                }
84
            }
85
        }
86
        
87
        chdir($old_cwd);
88
    }
89
90
    /**
91
     * Processes the validation group
92
     * 
93
     * @param array $data            
94
     * @return \Valitron\boolean
95
     */
96
    public function val(array $data)
97
    {
98
        $this->_fields = $data;
99
        $labels = array();
100
        foreach ($data as $key => $value) {
101
            $labels[$key] = ucwords(str_replace('_', ' ', $key));
102
        }
103
        
104
        $this->labels($labels);
105
        
106
        return parent::validate();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (validate() instead of val()). Are you sure this is correct? If so, you might want to change this to $this->validate().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
107
    }
108
109
    /**
110
     * Checks if there are errors and returns a boolean
111
     * 
112
     * @return boolean
113
     */
114
    public function hasErrors()
115
    {
116
        if (count($this->errors()) != '0') {
117
            return true;
118
        }
119
        
120
        return false;
121
    }
122
123
    /**
124
     * Returns the error messages
125
     * 
126
     * @return Ambigous <\Valitron\array, \Valitron\bool>
127
     */
128
    public function getErrorMessages()
129
    {
130
        return $this->errors();
131
    }
132
133
    /**
134
     * Returns an array of all the custom rules
135
     * 
136
     * @return array
137
     */
138
    public function getCustomRules()
139
    {
140
        return static::$_rules;
141
    }
142
143
    /**
144
     * Sets the regular expression engine
145
     * 
146
     * @param \mithra62\Regex $regex            
147
     * @return \mithra62\Validate
148
     */
149
    public function setRegex(\JaegerApp\Regex $regex)
150
    {
151
        $this->regex = $regex;
0 ignored issues
show
Documentation Bug introduced by
It seems like $regex of type object<JaegerApp\Regex> is incompatible with the declared type object<mithra62\Regex> of property $regex.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
152
        return $this;
153
    }
154
    
155
    public function getRegex()
156
    {
157
        return $this->regex;
158
    }
159
}