Constants::create()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace BFW\Helpers;
4
5
use \Exception;
6
7
/**
8
 * Helpers to manage constants
9
 */
10
class Constants
11
{
12
    /**
13
     * @const ERR_ALREADY_DEFINED Exception code if the constant is already
14
     * defined.
15
     */
16
    const ERR_ALREADY_DEFINED = 1602001;
17
    
18
    /**
19
     * Create a new constant if not exist
20
     * 
21
     * @param string $cstName The constant's name
22
     * @param mixed $cstValue The constant's value
23
     * 
24
     * @return void
25
     * 
26
     * @throws \Exception If the constant is already defined
27
     */
28
    public static function create(string $cstName, $cstValue)
29
    {
30
        if (defined($cstName)) {
31
            throw new Exception(
32
                'The constant '.$cstName.' is already defined.',
33
                self::ERR_ALREADY_DEFINED
34
            );
35
        }
36
        
37
        define($cstName, $cstValue);
38
    }
39
}
40