SendChatMessageCommand::execute()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.584
c 0
b 0
f 0
cc 3
nc 4
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Mremi\Flowdock library.
5
 *
6
 * (c) Rémi Marseille <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Mremi\Flowdock\Command;
13
14
use Mremi\Flowdock\Api\Push\ChatMessage;
15
use Mremi\Flowdock\Api\Push\Push;
16
17
use Symfony\Component\Console\Command\Command;
18
use Symfony\Component\Console\Input\InputArgument;
19
use Symfony\Component\Console\Input\InputInterface;
20
use Symfony\Component\Console\Input\InputOption;
21
use Symfony\Component\Console\Output\OutputInterface;
22
23
/**
24
 * Sends a message to a flow's chat
25
 *
26
 * @author Rémi Marseille <[email protected]>
27
 */
28
class SendChatMessageCommand extends Command
29
{
30
    /**
31
     * {@inheritdoc}
32
     */
33
    protected function configure()
34
    {
35
        $this
36
            ->setName('send-chat-message')
37
            ->setDescription('Sends a message to a flow\'s chat')
38
39
            ->addArgument('flow-api-token',     InputArgument::REQUIRED, 'The flow API token')
40
            ->addArgument('content',            InputArgument::REQUIRED, 'The content of the message')
41
            ->addArgument('external-user-name', InputArgument::REQUIRED, 'The name of the user sending the message')
42
43
            ->addOption('message-id', null, InputOption::VALUE_REQUIRED,                               'The identifier of an another message to comment')
44
            ->addOption('tags',       null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'The message tags')
45
            ->addOption('options',    null, InputOption::VALUE_REQUIRED,                               'An array of options used by request');
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    protected function execute(InputInterface $input, OutputInterface $output)
52
    {
53
        $push = new Push($input->getArgument('flow-api-token'));
0 ignored issues
show
Bug introduced by
It seems like $input->getArgument('flow-api-token') targeting Symfony\Component\Consol...nterface::getArgument() can also be of type array<integer,string> or null; however, Mremi\Flowdock\Api\Push\Push::__construct() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
54
55
        $message = ChatMessage::create()
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Mremi\Flowdock\Api\Push\BaseMessage as the method setExternalUserName() does only exist in the following sub-classes of Mremi\Flowdock\Api\Push\BaseMessage: Mremi\Flowdock\Api\Push\ChatMessage. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
56
            ->setContent($input->getArgument('content'))
0 ignored issues
show
Bug introduced by
It seems like $input->getArgument('content') targeting Symfony\Component\Consol...nterface::getArgument() can also be of type array<integer,string> or null; however, Mremi\Flowdock\Api\Push\BaseMessage::setContent() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
57
            ->setExternalUserName($input->getArgument('external-user-name'))
58
            ->setTags($input->getOption('tags'))
59
            ->setMessageId($input->getOption('message-id'));
60
61
        $options = $input->getOption('options') ? json_decode($input->getOption('options'), true) : array();
62
63
        if ($push->sendChatMessage($message, $options)) {
64
            $output->writeln('<info>Success:</info> the message has been sent');
65
66
            return;
67
        }
68
69
        $output->writeln(sprintf('<error>Failure:</error> %s', $message->getResponseMessage()));
70
        $output->writeln(var_export($message->getResponseErrors(), true));
71
    }
72
}
73