NumberValidator   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 29
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 29
loc 29
c 0
b 0
f 0
wmc 5
lcom 1
cbo 2
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 15 15 4
A client() 4 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 /** MicroNumberValidator */
2
3
namespace Micro\Validator;
4
5
use Micro\Form\IFormModel;
6
7
/**
8
 * NumberValidator class file.
9
 *
10
 * @author Oleg Lunegov <[email protected]>
11
 * @link https://github.com/linpax/microphp-framework
12
 * @copyright Copyright (c) 2013 Oleg Lunegov
13
 * @license https://github.com/linpax/microphp-framework/blob/master/LICENSE
14
 * @package Micro
15
 * @subpackage Validator
16
 * @version 1.0
17
 * @since 1.0
18
 */
19 View Code Duplication
class NumberValidator extends BaseValidator
0 ignored issues
show
Duplication introduced by
This class 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...
20
{
21
    /**
22
     * @inheritdoc
23
     */
24
    public function validate(IFormModel $model)
25
    {
26
        foreach ($this->elements AS $element) {
27
            if (!$model->checkAttributeExists($element)) {
28
                $this->errors[] = 'Parameter '.$element.' not defined in class '.get_class($model);
29
30
                return false;
31
            }
32
            if (!is_numeric($model->$element)) {
33
                $this->errors[] = 'Parameter '.$element.' is not a numeric';
34
            }
35
        }
36
37
        return true;
38
    }
39
40
    /**
41
     * @inheritdoc
42
     */
43
    public function client(IFormModel $model)
44
    {
45
        return 'if (! ((this.value ^ 0) === this.value) ) { e.preventDefault(); this.focus(); alert(\'Value is not number\'); }';
46
    }
47
}
48