Completed
Push — master ( c80620...0cd8d9 )
by Martijn
03:25
created

PreferContextTrait::getPreference()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Vanderlee\Comprehend\Core\Context;
4
5
use Vanderlee\Comprehend\Directive\Prefer;
6
7
trait PreferContextTrait
8
{
9
    private $preference = [];
10
11
    /**
12
     * @param $preference
13
     * @throws \DomainException
14
     */
15
    private static function assertPreference($preference)
16
    {
17
        if (!in_array($preference, [
18
            Prefer::FIRST,
19
            Prefer::LONGEST,
20
            Prefer::SHORTEST,
21
        ])) {
22
            throw new \DomainException("Invalid preference `{$preference}`");
23
        }
24
    }
25
26
    public function pushPreference($preference)
27
    {
28
        self::assertPreference($preference);
29
30
        array_push($this->preference, $preference);
31
    }
32
33
    public function popPreference()
34
    {
35
        array_pop($this->preference);
36
    }
37
38
    public function getPreference()
39
    {
40
        return end($this->preference);
41
    }
42
43
44
}