Robo::addInflectors()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.568
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Robo;
4
5
use Composer\Autoload\ClassLoader;
6
use League\Container\Container;
7
use League\Container\ContainerInterface;
8
use Robo\Common\ProcessExecutor;
9
use Consolidation\Config\ConfigInterface;
10
use Consolidation\Config\Loader\ConfigProcessor;
11
use Consolidation\Config\Loader\YamlConfigLoader;
12
use Symfony\Component\Console\Input\StringInput;
13
use Symfony\Component\Console\Application as SymfonyApplication;
14
use Symfony\Component\Process\Process;
15
16
/**
17
 * Manages the container reference and other static data.  Favor
18
 * using dependency injection wherever possible.  Avoid using
19
 * this class directly, unless setting up a custom DI container.
20
 */
21
class Robo
22
{
23
    const APPLICATION_NAME = 'Robo';
24
    const VERSION = '2.1.1-dev';
25
26
    /**
27
     * The currently active container object, or NULL if not initialized yet.
28
     *
29
     * @var \League\Container\ContainerInterface|null
30
     */
31
    protected static $container;
32
33
    /**
34
     * Entrypoint for standalone Robo-based tools.  See docs/framework.md.
35
     *
36
     * @param string[] $argv
37
     * @param string $commandClasses
38
     * @param null|string $appName
39
     * @param null|string $appVersion
40
     * @param null|\Symfony\Component\Console\Output\OutputInterface $output
41
     * @param null|string $repository
42
     *
43
     * @return int
44
     */
45
    public static function run($argv, $commandClasses, $appName = null, $appVersion = null, $output = null, $repository = null)
46
    {
47
        $runner = new \Robo\Runner($commandClasses);
48
        $runner->setSelfUpdateRepository($repository);
49
        $statusCode = $runner->execute($argv, $appName, $appVersion, $output);
50
        return $statusCode;
51
    }
52
53
    /**
54
     * Sets a new global container.
55
     *
56
     * @param \League\Container\ContainerInterface $container
57
     *   A new container instance to replace the current.
58
     */
59
    public static function setContainer(ContainerInterface $container)
60
    {
61
        static::$container = $container;
62
    }
63
64
    /**
65
     * Unsets the global container.
66
     */
67
    public static function unsetContainer()
68
    {
69
        static::$container = null;
70
    }
71
72
    /**
73
     * Returns the currently active global container.
74
     *
75
     * @return \League\Container\ContainerInterface
76
     *
77
     * @throws \RuntimeException
78
     */
79
    public static function getContainer()
80
    {
81
        if (static::$container === null) {
82
            throw new \RuntimeException('container is not initialized yet. \Robo\Robo::setContainer() must be called with a real container.');
83
        }
84
        return static::$container;
85
    }
86
87
    /**
88
     * Returns TRUE if the container has been initialized, FALSE otherwise.
89
     *
90
     * @return bool
91
     */
92
    public static function hasContainer()
93
    {
94
        return static::$container !== null;
95
    }
96
97
    /**
98
     * Create a config object and load it from the provided paths.
99
     *
100
     * @param string[] $paths
101
     *
102
     * @return \Consolidation\Config\ConfigInterface
103
     */
104
    public static function createConfiguration($paths)
105
    {
106
        $config = new \Robo\Config\Config();
107
        static::loadConfiguration($paths, $config);
108
        return $config;
109
    }
110
111
    /**
112
     * Use a simple config loader to load configuration values from specified paths
113
     *
114
     * @param string[] $paths
115
     * @param null|\Consolidation\Config\ConfigInterface $config
116
     */
117
    public static function loadConfiguration($paths, $config = null)
118
    {
119
        if ($config == null) {
120
            $config = static::config();
121
        }
122
        $loader = new YamlConfigLoader();
123
        $processor = new ConfigProcessor();
124
        $processor->add($config->export());
125
        foreach ($paths as $path) {
126
            $processor->extend($loader->load($path));
127
        }
128
        $config->import($processor->export());
0 ignored issues
show
Deprecated Code introduced by
The method Consolidation\Config\ConfigInterface::import() has been deprecated with message: Use 'replace'. Dflydev\DotAccessData\Data::import() merges, which is confusing, since this method replaces.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
129
    }
130
131
    /**
132
     * Create a container and initiailze it.  If you wish to *change*
133
     * anything defined in the container, then you should call
134
     * \Robo::configureContainer() instead of this function.
135
     *
136
     * @param null|\Symfony\Component\Console\Input\InputInterface $input
137
     * @param null|\Symfony\Component\Console\Output\OutputInterface $output
138
     * @param null|\Robo\Application $app
139
     * @param null|\Consolidation\Config\ConfigInterface $config
140
     * @param null|\Composer\Autoload\ClassLoader $classLoader
141
     *
142
     * @return \League\Container\Container|\League\Container\ContainerInterface
143
     */
144
    public static function createDefaultContainer($input = null, $output = null, $app = null, $config = null, $classLoader = null)
145
    {
146
        // Do not allow this function to be called more than once.
147
        if (static::hasContainer()) {
148
            return static::getContainer();
149
        }
150
151
        if (!$app) {
152
            $app = static::createDefaultApplication();
153
        }
154
155
        if (!$config) {
156
            $config = new \Robo\Config\Config();
157
        }
158
159
        // Set up our dependency injection container.
160
        $container = new Container();
161
        static::configureContainer($container, $app, $config, $input, $output, $classLoader);
162
163
        // Set the application dispatcher
164
        $app->setDispatcher($container->get('eventDispatcher'));
165
166
        return $container;
167
    }
168
169
    /**
170
     * Initialize a container with all of the default Robo services.
171
     * IMPORTANT:  after calling this method, clients MUST call:
172
     *
173
     * $dispatcher = $container->get('eventDispatcher');
174
     * $app->setDispatcher($dispatcher);
175
     *
176
     * Any modification to the container should be done prior to fetching
177
     * objects from it.
178
     *
179
     * It is recommended to use \Robo::createDefaultContainer()
180
     * instead, which does all required setup for the caller, but has
181
     * the limitation that the container it creates can only be
182
     * extended, not modified.
183
     *
184
     * @param \League\Container\ContainerInterface $container
185
     * @param \Symfony\Component\Console\Application $app
186
     * @param \Consolidation\Config\ConfigInterface $config
187
     * @param null|\Symfony\Component\Console\Input\InputInterface $input
188
     * @param null|\Symfony\Component\Console\Output\OutputInterface $output
189
     * @param null|\Composer\Autoload\ClassLoader $classLoader
190
     */
191
    public static function configureContainer(ContainerInterface $container, SymfonyApplication $app, ConfigInterface $config, $input = null, $output = null, $classLoader = null)
192
    {
193
        // Self-referential container refernce for the inflector
194
        $container->add('container', $container);
195
        static::setContainer($container);
196
197
        // Create default input and output objects if they were not provided
198
        if (!$input) {
199
            $input = new StringInput('');
200
        }
201
        if (!$output) {
202
            $output = new \Symfony\Component\Console\Output\ConsoleOutput();
203
        }
204
        if (!$classLoader) {
205
            $classLoader = new ClassLoader();
206
        }
207
        $config->set(Config::DECORATED, $output->isDecorated());
208
        $config->set(Config::INTERACTIVE, $input->isInteractive());
209
210
        $container->share('application', $app);
211
        $container->share('config', $config);
212
        $container->share('input', $input);
213
        $container->share('output', $output);
214
        $container->share('outputAdapter', \Robo\Common\OutputAdapter::class);
215
        $container->share('classLoader', $classLoader);
216
217
        // Register logging and related services.
218
        $container->share('logStyler', \Robo\Log\RoboLogStyle::class);
219
        $container->share('logger', \Robo\Log\RoboLogger::class)
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface League\Container\Definition\DefinitionInterface as the method withMethodCall() does only exist in the following implementations of said interface: League\Container\Definition\ClassDefinition.

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...
220
            ->withArgument('output')
221
            ->withMethodCall('setLogOutputStyler', ['logStyler']);
222
        $container->add('progressBar', \Symfony\Component\Console\Helper\ProgressBar::class)
223
            ->withArgument('output');
224
        $container->share('progressIndicator', \Robo\Common\ProgressIndicator::class)
225
            ->withArgument('progressBar')
226
            ->withArgument('output');
227
        $container->share('resultPrinter', \Robo\Log\ResultPrinter::class);
228
        $container->add('simulator', \Robo\Task\Simulator::class);
229
        $container->share('globalOptionsEventListener', \Robo\GlobalOptionsEventListener::class)
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface League\Container\Definition\DefinitionInterface as the method withMethodCall() does only exist in the following implementations of said interface: League\Container\Definition\ClassDefinition.

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...
230
            ->withMethodCall('setApplication', ['application']);
231
        $container->share('injectConfigEventListener', \Consolidation\Config\Inject\ConfigForCommand::class)
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface League\Container\Definition\DefinitionInterface as the method withMethodCall() does only exist in the following implementations of said interface: League\Container\Definition\ClassDefinition.

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...
232
            ->withArgument('config')
233
            ->withMethodCall('setApplication', ['application']);
234
        $container->share('collectionProcessHook', \Robo\Collection\CollectionProcessHook::class);
235
        $container->share('alterOptionsCommandEvent', \Consolidation\AnnotatedCommand\Options\AlterOptionsCommandEvent::class)
236
            ->withArgument('application');
237
        $container->share('hookManager', \Consolidation\AnnotatedCommand\Hooks\HookManager::class)
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface League\Container\Definition\DefinitionInterface as the method withMethodCall() does only exist in the following implementations of said interface: League\Container\Definition\ClassDefinition.

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...
238
            ->withMethodCall('addCommandEvent', ['alterOptionsCommandEvent'])
239
            ->withMethodCall('addCommandEvent', ['injectConfigEventListener'])
240
            ->withMethodCall('addCommandEvent', ['globalOptionsEventListener'])
241
            ->withMethodCall('addResultProcessor', ['collectionProcessHook', '*']);
242
        $container->share('eventDispatcher', \Symfony\Component\EventDispatcher\EventDispatcher::class)
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface League\Container\Definition\DefinitionInterface as the method withMethodCall() does only exist in the following implementations of said interface: League\Container\Definition\ClassDefinition.

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...
243
            ->withMethodCall('addSubscriber', ['hookManager']);
244
        $container->share('formatterManager', \Consolidation\OutputFormatters\FormatterManager::class)
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface League\Container\Definition\DefinitionInterface as the method withMethodCall() does only exist in the following implementations of said interface: League\Container\Definition\ClassDefinition.

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...
245
            ->withMethodCall('addDefaultFormatters', [])
246
            ->withMethodCall('addDefaultSimplifiers', []);
247
        $container->share('prepareTerminalWidthOption', \Consolidation\AnnotatedCommand\Options\PrepareTerminalWidthOption::class)
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface League\Container\Definition\DefinitionInterface as the method withMethodCall() does only exist in the following implementations of said interface: League\Container\Definition\ClassDefinition.

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...
248
            ->withMethodCall('setApplication', ['application']);
249
        $container->share('symfonyStyleInjector', \Robo\Symfony\SymfonyStyleInjector::class);
250
        $container->share('parameterInjection', \Consolidation\AnnotatedCommand\ParameterInjection::class)
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface League\Container\Definition\DefinitionInterface as the method withMethodCall() does only exist in the following implementations of said interface: League\Container\Definition\ClassDefinition.

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...
251
            ->withMethodCall('register', ['Symfony\Component\Console\Style\SymfonyStyle', 'symfonyStyleInjector']);
252
        $container->share('commandProcessor', \Consolidation\AnnotatedCommand\CommandProcessor::class)
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface League\Container\Definition\DefinitionInterface as the method withMethodCall() does only exist in the following implementations of said interface: League\Container\Definition\ClassDefinition.

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...
253
            ->withArgument('hookManager')
254
            ->withMethodCall('setFormatterManager', ['formatterManager'])
255
            ->withMethodCall('addPrepareFormatter', ['prepareTerminalWidthOption'])
256
            ->withMethodCall('setParameterInjection', ['parameterInjection'])
257
            ->withMethodCall(
258
                'setDisplayErrorFunction',
259
                [
260
                    function ($output, $message) use ($container) {
261
                        $logger = $container->get('logger');
262
                        $logger->error($message);
263
                    }
264
                ]
265
            );
266
        $container->share('stdinHandler', \Consolidation\AnnotatedCommand\Input\StdinHandler::class);
267
        $container->share('commandFactory', \Consolidation\AnnotatedCommand\AnnotatedCommandFactory::class)
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface League\Container\Definition\DefinitionInterface as the method withMethodCall() does only exist in the following implementations of said interface: League\Container\Definition\ClassDefinition.

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...
268
            ->withMethodCall('setCommandProcessor', ['commandProcessor']);
269
        $container->share('relativeNamespaceDiscovery', \Robo\ClassDiscovery\RelativeNamespaceDiscovery::class)
270
            ->withArgument('classLoader');
271
272
        // Deprecated: favor using collection builders to direct use of collections.
273
        $container->add('collection', \Robo\Collection\Collection::class);
274
        // Deprecated: use CollectionBuilder::create() instead -- or, better
275
        // yet, BuilderAwareInterface::collectionBuilder() if available.
276
        $container->add('collectionBuilder', \Robo\Collection\CollectionBuilder::class);
277
278
        static::addInflectors($container);
279
280
        // Make sure the application is appropriately initialized.
281
        $app->setAutoExit(false);
282
    }
283
284
    /**
285
     * @param null|string $appName
286
     * @param null|string $appVersion
287
     *
288
     * @return \Robo\Application
289
     */
290
    public static function createDefaultApplication($appName = null, $appVersion = null)
291
    {
292
        $appName = $appName ?: self::APPLICATION_NAME;
293
        $appVersion = $appVersion ?: self::VERSION;
294
295
        $app = new \Robo\Application($appName, $appVersion);
296
        $app->setAutoExit(false);
297
        return $app;
298
    }
299
300
    /**
301
     * Add the Robo League\Container inflectors to the container
302
     *
303
     * @param \League\Container\ContainerInterface $container
304
     */
305
    public static function addInflectors($container)
306
    {
307
        // Register our various inflectors.
308
        $container->inflector(\Robo\Contract\ConfigAwareInterface::class)
309
            ->invokeMethod('setConfig', ['config']);
310
        $container->inflector(\Psr\Log\LoggerAwareInterface::class)
311
            ->invokeMethod('setLogger', ['logger']);
312
        $container->inflector(\League\Container\ContainerAwareInterface::class)
313
            ->invokeMethod('setContainer', ['container']);
314
        $container->inflector(\Symfony\Component\Console\Input\InputAwareInterface::class)
315
            ->invokeMethod('setInput', ['input']);
316
        $container->inflector(\Robo\Contract\OutputAwareInterface::class)
317
            ->invokeMethod('setOutput', ['output']);
318
        $container->inflector(\Robo\Contract\ProgressIndicatorAwareInterface::class)
319
            ->invokeMethod('setProgressIndicator', ['progressIndicator']);
320
        $container->inflector(\Consolidation\AnnotatedCommand\Events\CustomEventAwareInterface::class)
321
            ->invokeMethod('setHookManager', ['hookManager']);
322
        $container->inflector(\Robo\Contract\VerbosityThresholdInterface::class)
323
            ->invokeMethod('setOutputAdapter', ['outputAdapter']);
324
        $container->inflector(\Consolidation\AnnotatedCommand\Input\StdinAwareInterface::class)
325
            ->invokeMethod('setStdinHandler', ['stdinHandler']);
326
    }
327
328
    /**
329
     * Retrieves a service from the container.
330
     *
331
     * Use this method if the desired service is not one of those with a dedicated
332
     * accessor method below. If it is listed below, those methods are preferred
333
     * as they can return useful type hints.
334
     *
335
     * @param string $id
336
     *   The ID of the service to retrieve.
337
     *
338
     * @return mixed
339
     *   The specified service.
340
     */
341
    public static function service($id)
342
    {
343
        return static::getContainer()->get($id);
344
    }
345
346
    /**
347
     * Indicates if a service is defined in the container.
348
     *
349
     * @param string $id
350
     *   The ID of the service to check.
351
     *
352
     * @return bool
353
     *   TRUE if the specified service exists, FALSE otherwise.
354
     */
355
    public static function hasService($id)
356
    {
357
        // Check hasContainer() first in order to always return a Boolean.
358
        return static::hasContainer() && static::getContainer()->has($id);
359
    }
360
361
    /**
362
     * Return the result printer object.
363
     *
364
     * @return \Robo\Log\ResultPrinter
365
     */
366
    public static function resultPrinter()
367
    {
368
        return static::service('resultPrinter');
369
    }
370
371
    /**
372
     * @return \Consolidation\Config\ConfigInterface
373
     */
374
    public static function config()
375
    {
376
        return static::service('config');
377
    }
378
379
    /**
380
     * @return \Consolidation\Log\Logger
381
     */
382
    public static function logger()
383
    {
384
        return static::service('logger');
385
    }
386
387
    /**
388
     * @return \Robo\Application
389
     */
390
    public static function application()
391
    {
392
        return static::service('application');
393
    }
394
395
    /**
396
     * Return the output object.
397
     *
398
     * @return \Symfony\Component\Console\Output\OutputInterface
399
     */
400
    public static function output()
401
    {
402
        return static::service('output');
403
    }
404
405
    /**
406
     * Return the input object.
407
     *
408
     * @return \Symfony\Component\Console\Input\InputInterface
409
     */
410
    public static function input()
411
    {
412
        return static::service('input');
413
    }
414
415
    /**
416
     * @return \Robo\Common\ProcessExecutor
417
     */
418
    public static function process(Process $process)
419
    {
420
        return ProcessExecutor::create(static::getContainer(), $process);
421
    }
422
}
423