UsesInterfaceTrait::requireInterface()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
/**
3
 * This file is part of the phpspec-behavior package.
4
 * (c) 2017 Timo Michna <timomichna/yahoo.de>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Tidal\PhpSpec\BehaviorExtension\Behavior\Console\Command;
11
12
use Throwable;
13
use Tidal\PhpSpec\BehaviorExtension\Exception\NoInterfaceException;
14
use Tidal\PhpSpec\ConsoleExtension\Contract\WriterInterface;
15
16
/**
17
 * trait Tidal\PhpSpec\BehaviorExtension\Behavior\Console\Command\UsesInterfaceTrait
18
 */
19
trait UsesInterfaceTrait
20
{
21
    /**
22
     * @param string $interfaceName
23
     * @return bool
24
     */
25
    public function validateInterface(string $interfaceName)
26
    {
27
        return interface_exists(str_replace('/', '\\', $interfaceName));
28
    }
29
30
    /**
31
     * @param string $interfaceName
32
     */
33
    public function requireInterface(string $interfaceName)
34
    {
35
        if (!$this->validateInterface($interfaceName)) {
36
            throw new NoInterfaceException($interfaceName);
37
        }
38
    }
39
40
    /**
41
     * @param string $interfaceName
42
     * @throws Throwable
43
     */
44
    public function demandInterface(string $interfaceName)
45
    {
46
        try {
47
            $this->requireInterface($interfaceName);
48
        } catch (Throwable $exception) {
0 ignored issues
show
Bug introduced by
The class Throwable does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
49
            $this->getWriter()->writeError($exception->getMessage());
50
51
            throw $exception;
52
        }
53
    }
54
55
    /**
56
     * @param WriterInterface $writer
57
     */
58
    abstract public function setWriter(WriterInterface $writer);
59
60
    /**
61
     * @return WriterInterface
62
     */
63
    abstract public function getWriter(): WriterInterface;
64
}
65