EnvironmentCompletionContext   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 41
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 19 3
A useWordBreaksFromEnvironment() 0 10 2
1
<?php
2
3
4
namespace Stecman\Component\Symfony\Console\BashCompletion;
5
6
class EnvironmentCompletionContext extends CompletionContext
7
{
8
    /**
9
     * Set up completion context from the environment variables set by the parent shell
10
     */
11
    public function __construct()
12
    {
13
        $this->commandLine = getenv('CMDLINE_CONTENTS');
14
        $this->charIndex = intval(getenv('CMDLINE_CURSOR_INDEX'));
15
16
        if ($this->commandLine === false) {
17
            $message = 'Failed to configure from environment; Environment var CMDLINE_CONTENTS not set.';
18
19
            if (getenv('COMP_LINE')) {
20
                $message .= "\n\nYou appear to be attempting completion using an out-dated hook. If you've just updated,"
21
                            . " you probably need to reinitialise the completion shell hook by reloading your shell"
22
                            . " profile or starting a new shell session. If you are using a hard-coded (rather than generated)"
23
                            . " hook, you will need to update that function with the new environment variable names."
24
                            . "\n\nSee here for details: https://github.com/stecman/symfony-console-completion/issues/31";
25
            }
26
27
            throw new \RuntimeException($message);
28
        }
29
    }
30
31
    /**
32
     * Use the word break characters set by the parent shell.
33
     *
34
     * @throws \RuntimeException
35
     */
36
    public function useWordBreaksFromEnvironment()
37
    {
38
        $breaks = getenv('CMDLINE_WORDBREAKS');
39
40
        if (!$breaks) {
41
            throw new \RuntimeException('Failed to read word breaks from environment; Environment var CMDLINE_WORDBREAKS not set');
42
        }
43
44
        $this->wordBreaks = $breaks;
45
    }
46
}
47