Passed
Push — master ( 310acb...1a0150 )
by Scott
02:48
created

Help::expectFunction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
crap 1
1
<?php
2
namespace Desmond\functions\core;
3
use Desmond\functions\DesmondFunction;
4
use Desmond\ArgumentHelper;
5
use Desmond\functions\DocLibrary;
6
use Desmond\data_types\StringType;
7
use Desmond\exceptions\ArgumentException;
8
9
class Help extends DesmondFunction
10
{
11
    use ArgumentHelper;
12
13 187
    public function id()
14
    {
15 187
        return 'help';
16
    }
17
18 3
    public function run(array $args)
19
    {
20 3
        $this->expectFunction($args);
21 2
        $library = new DocLibrary();
22 2
        $library->index();
23 2
        if (!isset($library->library()[$args[0]->value()])) {
24 1
            throw new ArgumentException('"help": Function "' . $args[0]->value() . '" not found.');
25
        }
26 1
        $doc = $library->library()[$args[0]->value()];
27 1
        return self::formatHelpText($doc);
28
    }
29
30 3
    private function expectFunction($args)
31
    {
32 3
        $this->expectArguments(
33 3
            'help',
34 3
            [0 => ['String']],
35
            $args
36
        );
37 2
    }
38
39 1
    private static function formatHelpText($doc)
40
    {
41 1
        $help = '';
42 1
        $help .= $doc->synopsis() . "\n";
43 1
        $help .= "Usage: {$doc->usage()}\n";
44 1
        $help .= "Examples:\n";
45 1
        foreach ($doc->examples() as $example) {
46 1
            $help .= "\t$example\n";
47
        }
48 1
        return new StringType($help);
49
    }
50
}
51