PreferContextTrait   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 11
dl 0
loc 35
ccs 14
cts 14
cp 1
rs 10
c 1
b 1
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A assertPreference() 0 8 2
A pushPreference() 0 5 1
A getPreference() 0 3 1
A popPreference() 0 3 1
1
<?php
2
3
namespace Vanderlee\Comprehend\Core\Context;
4
5
use DomainException;
6
use Vanderlee\Comprehend\Directive\Prefer;
7
8
trait PreferContextTrait
9
{
10
    private $preference = [];
11
12
    /**
13
     * @param $preference
14
     *
15
     * @throws DomainException
16
     */
17 514
    private static function assertPreference($preference)
18
    {
19 514
        if (!in_array($preference, [
20 514
            Prefer::FIRST,
21
            Prefer::LONGEST,
22
            Prefer::SHORTEST,
23
        ])) {
24 1
            throw new DomainException('Invalid preference `' . $preference . '`');
25
        }
26 513
    }
27
28 513
    public function pushPreference($preference)
29
    {
30 513
        self::assertPreference($preference);
31
32 513
        $this->preference[] = $preference;
33 513
    }
34
35 42
    public function popPreference()
36
    {
37 42
        array_pop($this->preference);
38 42
    }
39
40 104
    public function getPreference()
41
    {
42 104
        return end($this->preference);
43
    }
44
}
45