PoolProvider   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 5
dl 0
loc 41
ccs 0
cts 24
cp 0
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A register() 0 34 4
1
<?php
2
3
namespace Basis\Provider;
4
5
use Basis\Service;
6
use League\Container\ServiceProvider\AbstractServiceProvider;
7
use Tarantool\Client\Client;
8
use Tarantool\Mapper\Mapper;
9
use Tarantool\Mapper\Plugin\Sequence;
10
use Tarantool\Mapper\Plugin\Spy;
11
use Tarantool\Mapper\Plugin\Temporal;
12
use Tarantool\Mapper\Pool;
13
14
class PoolProvider extends AbstractServiceProvider
15
{
16
    protected $provides = [
17
        Pool::class,
18
    ];
19
20
    public function register()
21
    {
22
        $this->getContainer()->share(Pool::class, function () {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Psr\Container\ContainerInterface as the method share() does only exist in the following implementations of said interface: Basis\Application, Basis\Test\Application, League\Container\Container.

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...
23
            $pool = new Pool();
24
            $container = $this->getContainer();
25
            $pool->registerResolver(function ($name) use ($container) {
26
27
                if ($name == 'default' || $name == $container->get(Service::class)->getName()) {
28
                    $mapper = $container->get(Mapper::class);
29
                    $mapper->serviceName = $container->get(Service::class)->getName();
30
                    return $mapper;
31
                }
32
33
                $service = $container->get(Service::class);
34
35
                if (in_array($name, $service->listServices())) {
36
                    $address = $service->getHost($name.'-db')->address;
37
                    $client = Client::fromDsn('tcp://'.$address.':3301');
38
                    $client->evaluate("box.session.su('admin')");
39
                    $mapper = new Mapper($client);
40
                    $mapper->getPlugin(Sequence::class);
41
                    $mapper->getPlugin(Spy::class);
42
                    $mapper->getPlugin(Temporal::class)
43
                        ->getAggregator()
44
                        ->setReferenceAggregation(false);
45
46
                    $mapper->serviceName = $name;
0 ignored issues
show
Bug introduced by
The property serviceName does not seem to exist in Tarantool\Mapper\Mapper.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
47
                    return $mapper;
48
                }
49
            });
50
51
            return $pool;
52
        });
53
    }
54
}
55