Alpha   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 47
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 0
dl 47
loc 47
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 4 4 2
A validate() 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
2
/**
3
 * Particle.
4
 *
5
 * @link      http://github.com/particle-php for the canonical source repository
6
 * @copyright Copyright (c) 2005-2016 Particle (http://particle-php.com)
7
 * @license   https://github.com/particle-php/validator/blob/master/LICENSE New BSD License
8
 */
9
namespace Particle\Validator\Rule;
10
11
/**
12
 * This rule checks if the value consists solely out of alphabetic characters.
13
 *
14
 * @package Particle\Validator\Rule
15
 */
16 View Code Duplication
class Alpha extends Regex
17
{
18
    /**
19
     * A constant that will be used for the error message when the value contains non-alphabetic characters.
20
     */
21
    const NOT_ALPHA = 'Alpha::NOT_ALPHA';
22
23
    /**
24
     * A constant indicated spaces are allowed.
25
     */
26
    const ALLOW_SPACES = true;
27
28
    /**
29
     * A constant indicating spaces are *not* allowed.
30
     */
31
    const DISALLOW_SPACES = false;
32
33
    /**
34
     * The message templates which can be returned by this validator.
35
     *
36
     * @var array
37
     */
38
    protected $messageTemplates = [
39
        self::NOT_ALPHA => '{{ name }} may only consist out of alphabetic characters'
40
    ];
41
42
    /**
43
     * Construct the Alpha rule.
44
     *
45
     * @param bool $allowWhitespace
46
     */
47 7
    public function __construct($allowWhitespace = self::DISALLOW_SPACES)
48
    {
49 7
        parent::__construct($allowWhitespace ? '~^[\p{L}\s]*$~iu' : '~^[\p{L}]*$~ui');
50 7
    }
51
52
    /**
53
     * Checks whether $value consists solely out of alphabetic characters.
54
     *
55
     * @param mixed $value
56
     * @return bool
57
     */
58 7
    public function validate($value)
59
    {
60 7
        return $this->match($this->regex, $value, self::NOT_ALPHA);
61
    }
62
}
63