PreferContextTrait::pushPreference()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
c 1
b 1
f 0
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