Completed
Push — develop ( 12ee62...64859f )
by Arkadiusz
02:45
created

LayeredNetwork   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 56
c 0
b 0
f 0
wmc 7
lcom 1
cbo 2
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A addLayer() 0 4 1
A getLayers() 0 4 1
A getOutputLayer() 0 4 1
A getOutput() 0 9 2
A setInput() 0 8 2
1
<?php
2
3
declare (strict_types = 1);
4
5
namespace Phpml\NeuralNetwork\Network;
6
7
use Phpml\NeuralNetwork\Layer;
8
use Phpml\NeuralNetwork\Network;
9
10
abstract class LayeredNetwork implements Network
11
{
12
    /**
13
     * @var Layer[]
14
     */
15
    protected $layers;
16
17
    /**
18
     * @param Layer $layer
19
     */
20
    public function addLayer(Layer $layer)
21
    {
22
        $this->layers[] = $layer;
23
    }
24
25
    /**
26
     * @return Layer[]
27
     */
28
    public function getLayers(): array
29
    {
30
        return $this->layers;
31
    }
32
33
    /**
34
     * @return Layer
35
     */
36
    public function getOutputLayer(): Layer
37
    {
38
        return $this->layers[count($this->layers) - 1];
39
    }
40
41
    /**
42
     * @return array
43
     */
44
    public function getOutput(): array
45
    {
46
        $result = [];
47
        foreach ($this->getOutputLayer()->getNodes() as $neuron) {
48
            $result[] = $neuron->getOutput();
49
        }
50
51
        return $result;
52
    }
53
54
    /**
55
     * @param mixed $input
56
     */
57
    public function setInput($input)
58
    {
59
        $firstLayer = $this->layers[0];
60
61
        foreach ($firstLayer->getNodes() as $key => $neuron) {
62
            $neuron->setInput($input[$key]);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Phpml\NeuralNetwork\Node as the method setInput() does only exist in the following implementations of said interface: Phpml\NeuralNetwork\Node\Input.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
63
        }
64
    }
65
}
66