Completed
Push — master ( 14784f...87aa66 )
by Richard
05:56
created

RulePrinter::print_tail()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4.8437

Importance

Changes 2
Bugs 0 Features 1
Metric Value
dl 0
loc 13
ccs 5
cts 8
cp 0.625
rs 9.2
c 2
b 0
f 1
nc 4
cc 4
eloc 8
nop 1
crap 4.8437
1
<?php
2
/******************************************************************************
3
 * An implementation of dicto (scg.unibe.ch/dicto) in and for PHP.
4
 * 
5
 * Copyright (c) 2016 Richard Klees <[email protected]>
6
 *
7
 * This software is licensed under The MIT License. You should have received 
8
 * a copy of the licence along with the code.
9
 */
10
11
namespace Lechimp\Dicto\Output;
12
use Lechimp\Dicto\Definition as Def;
13
14
/**
15
 * Prints rules.
16
 */
17
class RulePrinter {
18
    /**
19
     * @param   Def\Rules\Rule  $rule
20
     * @return  string
21
     */ 
22 3
    public function pprint(Def\Rules\Rule $rule) {
23 3
        return $this->print_head($rule).$this->print_tail($rule);
24
    }
25
26 3
    protected function print_head(Def\Rules\Rule $rule) {
27 3
        $name = $rule->subject()->name();
28 3
        switch ($rule->mode()) {
29 3
            case Def\Rules\Rule::MODE_CANNOT:
30 2
                return "$name cannot ";
31 1
            case Def\Rules\Rule::MODE_MUST:
32 1
                return "$name must ";
33
            case Def\Rules\Rule::MODE_ONLY_CAN:
34
                return "only $name can ";
35
            default:
36
                throw new \Exception("Unknown rule mode '".$rule->mode()."'");
37
        } 
38
    }
39
40 3
    protected function print_tail(Def\Rules\Rule $rule) {
41 3
        if ($rule instanceof Def\Rules\Invoke) {
42 3
            return "invoke ".$rule->invokes()->name();
43
        }
44 1
        if($rule instanceof Def\Rules\DependOn) {
45 1
            return "depend on ".$rule->dependency()->name();
46
        }
47
        if ($rule instanceof Def\Rules\ContainText) {
48
            return "contain text \"".$rule->regexp()."\"";
49
        }
50
51
        throw new \Exception("Unknown rule '".$cls."'");
52
    }
53
}
54