ComponentTestHelperTrait   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 81
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A connectInPorts() 0 8 2
B connectOutPorts() 0 28 2
A getOutPortData() 0 10 3
A wasCalled() 0 4 1
1
<?php
2
/*
3
 * This file is part of the phpflo/core package.
4
 *
5
 * (c) Marc Aschmann <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace PhpFlo\Test;
11
12
use PhpFlo\Common\ComponentInterface;
13
use PhpFlo\Interaction\InternalSocket;
14
15
/**
16
 * Class ComponentTestHelperTrait
17
 *
18
 * @package PhpFlo\PhpFloBundle\Test
19
 * @author Marc Aschmann <[email protected]>
20
 */
21
trait ComponentTestHelperTrait
22
{
23
    /**
24
     * @var array
25
     */
26
    private $outPortSockets;
27
28
    /**
29
     * Fake-connect socket to port.
30
     *
31
     * @param ComponentInterface $component
32
     * @return ComponentInterface
33
     */
34
    protected function connectInPorts(ComponentInterface $component)
35
    {
36
        foreach ($component->inPorts() as $alias => $inPort) {
37
            $inPort->attach($this->stub(InternalSocket::class));
0 ignored issues
show
Bug introduced by
It seems like stub() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
38
        }
39
40
        return $component;
41
    }
42
43
    /**
44
     * Fake-connect socket to port and add a storage for later value checks.
45
     *
46
     * @param ComponentInterface $component
47
     * @return ComponentInterface
48
     */
49
    protected function connectOutPorts(ComponentInterface $component)
50
    {
51
        $this->outPortSockets = [];
52
        foreach ($component->outPorts() as $port) {
53
            $socket = $this->stub(
0 ignored issues
show
Bug introduced by
It seems like stub() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
54
                InternalSocket::class,
55
                [
56
                    'isConnected' => true,
57
                ]
58
            );
59
            $socket->expects($this->any())
0 ignored issues
show
Bug introduced by
It seems like any() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
60
                ->method('send')
61
                ->willReturnCallback(
62
                    \Closure::bind(
63
                        function ($data) {
64
                            $this->data = $data;
0 ignored issues
show
Bug introduced by
The property data does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
65
                        },
66
                        $socket
67
                    )
68
                );
69
            $this->outPortSockets[$port->getName()] = $socket;
70
            $socket->from = [];
71
            $socket->to = [];
72
            $port->attach($socket);
73
        }
74
75
        return $component;
76
    }
77
78
    /**
79
     * @param string $port
80
     * @return array|mixed
81
     */
82
    protected function getOutPortData($port = '')
83
    {
84
        if ('' !== $port) {
85
            if (array_key_exists($port, $this->outPortSockets)) {
86
                return $this->outPortSockets[$port]->data;
87
            }
88
        }
89
90
        return $this->outPortSockets;
91
    }
92
93
    /**
94
     * @param string $port
95
     * @return bool
96
     */
97
    protected function wasCalled($port)
98
    {
99
        return !empty($this->outPortSockets[$port]);
100
    }
101
}
102