GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 4a329e...1320b2 )
by Lukáš
03:00
created

GenerateProxiesCommand   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 18
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 5 1
A execute() 0 9 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Lookyman\Nette\Proxy\Console;
5
6
use Lookyman\Nette\Proxy\DI\ProxyExtension;
7
use Nette\DI\Container;
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
class GenerateProxiesCommand extends Command
13
{
14
	protected function configure()
15
	{
16
		$this->setName('lookyman:nette-proxy:generate')
17
			->setDescription('Generate proxies for lazy services');
18
	}
19
20
	protected function execute(InputInterface $input, OutputInterface $output)
21
	{
22
		/** @var Container $container */
23
		$container = $this->getHelper('container')->getContainer();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Symfony\Component\Console\Helper\HelperInterface as the method getContainer() does only exist in the following implementations of said interface: Kdyby\Console\ContainerHelper.

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...
24
		foreach (array_keys($container->findByTag(ProxyExtension::TAG_LAZY)) as $name) {
25
			$container->getService($name);
26
			$output->writeln(sprintf('Proxy for service %s generated', $name));
27
		}
28
	}
29
}
30