Completed
Pull Request — master (#29)
by Aleh
02:59
created

CompleteCommand::configure()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 26
rs 8.8571
cc 1
eloc 23
nc 1
nop 0
1
<?php
2
3
namespace Padawan\Command;
4
5
use Padawan\Framework\Complete\CompleteEngine;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use Padawan\Domain\ProjectRepository;
10
use Padawan\Framework\Project\Persister;
11
12
class CompleteCommand extends AsyncCommand
13
{
14
15
    protected function configure()
16
    {
17
        $this->setName("complete")
18
            ->setDescription("Finds completion")
19
            ->addArgument(
20
                "path",
21
                InputArgument::REQUIRED,
22
                "Path to the project root"
23
            )->addArgument(
24
                "column",
25
                InputArgument::REQUIRED,
26
                "Column number of cursor position"
27
            )->addArgument(
28
                "line",
29
                InputArgument::REQUIRED,
30
                "Line number of cursor position"
31
            )->addArgument(
32
                "data",
33
                InputArgument::REQUIRED,
34
                "File contents"
35
            )->addArgument(
36
                "filepath",
37
                InputArgument::REQUIRED,
38
                "Path to file relative to project root"
39
            );
40
    }
41
    protected function execute(InputInterface $input, OutputInterface $output)
42
    {
43
        $column = $input->getArgument("column");
44
        $file = $input->getArgument("filepath");
45
        $line = $input->getArgument("line");
46
        $content = $input->getArgument("data");
47
        $path = $input->getArgument("path");
48
        $container = $this->getContainer();
0 ignored issues
show
Unused Code introduced by
$container is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
49
50
        $projectRepository = $this->getContainer()->get(ProjectRepository::class);
51
        $project = $projectRepository->findByPath($path);
52
53
        $completeEngine = $this->getContainer()->get(CompleteEngine::class);
54
        /** @var Persister */
55
        $persister = $this->getContainer()->get(Persister::class);
56
        try {
57
            $completion = $completeEngine->createCompletion(
58
                $project,
59
                $content,
60
                $line,
61
                $column,
62
                $file
63
            );
64
65
            yield $output->write(
66
                json_encode(
67
                    [
68
                        "completion" => $this->prepareEntries(
69
                            $completion["entries"]
70
                        ),
71
                        "context" => $completion["context"]
72
                    ]
73
                )
74
            );
75
            yield $output->disconnect();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Symfony\Component\Console\Output\OutputInterface as the method disconnect() does only exist in the following implementations of said interface: Padawan\Framework\Application\Socket\SocketOutput.

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...
76
            yield $persister->save($project);
77
        } catch (\Exception $e) {
78
            yield $output->write(
79
                json_encode(
80
                    [
81
                        "completion" => [],
82
                        "context" => []
83
                    ]
84
                )
85
            );
86
        }
87
    }
88
    protected function prepareEntries(array $entries) {
89
        $result = [];
90
        foreach ($entries as $entry) {
91
            $result[] = [
92
                "name" => $entry->getName(),
93
                "signature" => $entry->getSignature(),
94
                "description" => $entry->getDesc(),
95
                "menu" => $entry->getMenu()
96
            ];
97
        }
98
        return $result;
99
    }
100
}
101