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

PreferContextTrait   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 34
rs 10
c 0
b 0
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 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
}