Passed
Push — 1.0 ( 12d132...dd1957 )
by Vladimir
06:10
created

Transitions   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
c 0
b 0
f 0
lcom 1
cbo 3
dl 0
loc 62
ccs 23
cts 23
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A jump() 0 14 2
B restart() 0 24 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FondBot\Conversation\Traits;
6
7
use RuntimeException;
8
use InvalidArgumentException;
9
use FondBot\Application\Kernel;
10
use FondBot\Conversation\Intent;
11
use FondBot\Conversation\Interaction;
12
13
trait Transitions
14
{
15
    /** @var Kernel */
16
    protected $kernel;
17
18
    /**
19
     * Whether any transition run.
20
     *
21
     * @var bool
22
     */
23
    protected $transitioned = false;
24
25
    /**
26
     * Jump to another interaction.
27
     *
28
     * @param string $interaction
29
     *
30
     * @throws \InvalidArgumentException
31
     */
32 3
    protected function jump(string $interaction): void
33
    {
34
        /** @var Interaction $instance */
35 3
        $instance = $this->kernel->resolve($interaction);
36
37 3
        if (!$instance instanceof Interaction) {
38 2
            throw new InvalidArgumentException('Invalid interaction `'.$interaction.'`');
39
        }
40
41
        // Run interaction
42 1
        $this->kernel->converse($instance);
43
44 1
        $this->transitioned = true;
45 1
    }
46
47
    /**
48
     * Restart current intent or interaction.
49
     */
50 3
    protected function restart(): void
51
    {
52
        switch (true) {
53 3
            case $this instanceof Intent:
54 1
                $this->kernel->clearContext();
0 ignored issues
show
Bug introduced by
The property kernel cannot be accessed from this context as it is declared protected in class FondBot\Conversation\Traits\InteractsWithContext.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
55
56 1
                $this->kernel->converse($this);
0 ignored issues
show
Bug introduced by
The property kernel cannot be accessed from this context as it is declared protected in class FondBot\Conversation\Traits\InteractsWithContext.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
57
58 1
                $this->transitioned = true;
0 ignored issues
show
Bug introduced by
The property transitioned cannot be accessed from this context as it is declared protected in class FondBot\Conversation\Traits\Transitions.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
59 1
                break;
60 1
            case $this instanceof Interaction:
61 1
                $context = $this->kernel->getContext();
0 ignored issues
show
Bug introduced by
The property kernel cannot be accessed from this context as it is declared protected in class FondBot\Conversation\Traits\InteractsWithContext.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
62 1
                $context->setInteraction(null);
63 1
                $context->setValues([]);
64 1
                $this->kernel->setContext($context);
0 ignored issues
show
Bug introduced by
The property kernel cannot be accessed from this context as it is declared protected in class FondBot\Conversation\Traits\InteractsWithContext.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
Bug introduced by
It seems like $context defined by $this->kernel->getContext() on line 61 can be null; however, FondBot\Application\Kernel::setContext() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
65
66 1
                $this->transitioned = true;
0 ignored issues
show
Bug introduced by
The property transitioned cannot be accessed from this context as it is declared protected in class FondBot\Conversation\Traits\Transitions.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
67
68 1
                $this->kernel->converse($this);
0 ignored issues
show
Bug introduced by
The property kernel cannot be accessed from this context as it is declared protected in class FondBot\Conversation\Traits\InteractsWithContext.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
69 1
                break;
70
            default:
71 1
                throw new RuntimeException('Only conversable instances can be restarted.');
72
        }
73 2
    }
74
}
75