Passed
Push — feature/config-file ( 80a3c9...c343d6 )
by Arnaud
11:33 queued 07:45
created

AbstractCommand::getConfigFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the Cecil/Cecil package.
4
 *
5
 * Copyright (c) Arnaud Ligny <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Cecil\Command;
12
13
use Cecil\Builder;
14
use Cecil\Logger\ConsoleLogger;
15
use Cecil\Util;
16
use Symfony\Component\Console\Command\Command;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Output\OutputInterface;
19
use Symfony\Component\Console\Style\SymfonyStyle;
20
use Symfony\Component\Filesystem\Filesystem;
21
use Symfony\Component\Yaml\Exception\ParseException;
22
use Symfony\Component\Yaml\Yaml;
23
24
class AbstractCommand extends Command
25
{
26
    const CONFIG_FILE = 'config.yml';
27
    const TMP_DIR = '.cecil';
28
29
    /** @var InputInterface */
30
    protected $input;
31
    /** @var OutputInterface */
32
    protected $output;
33
    /** @var SymfonyStyle */
34
    protected $io;
35
    /** @var Filesystem */
36
    protected $fs;
37
    /** @var string */
38
    protected $path;
39
    /** @var string */
40
    protected $configFile;
41
    /** @var Builder */
42
    protected $builder;
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    protected function initialize(InputInterface $input, OutputInterface $output)
48
    {
49
        $this->input = $input;
50
        $this->output = $output;
51
        $this->io = new SymfonyStyle($input, $output);
52
        $this->fs = new Filesystem();
53
54
        if (!in_array($this->getName(), ['self-update'])) {
55
            // working directory
56
            $this->path = $input->getArgument('path');
0 ignored issues
show
Documentation Bug introduced by
It seems like $input->getArgument('path') can also be of type string[]. However, the property $path is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
57
            if (null === $this->getPath()) {
58
                $this->path = getcwd();
59
            }
60
            if (false === realpath($this->getPath())) {
61
                $this->fs->mkdir($this->getPath());
62
            }
63
            $this->path = realpath($this->getPath());
64
            // config file
65
            $this->configFile = $input->getOption('config');
66
            if (null === $this->getConfigFile()) {
67
                $this->configFile = Util::joinFile($this->getPath(), self::CONFIG_FILE);
68
            }
69
            $this->configFile = realpath($this->getConfigFile());
70
            // checks config file
71
            if (!in_array($this->getName(), ['new:site'])) {
72
                if (false === $this->configFile) {
73
                    $message = sprintf('Could not find "%s": uses default configuration.', $input->getOption('config'));
0 ignored issues
show
Bug introduced by
It seems like $input->getOption('config') can also be of type string[]; however, parameter $args of sprintf() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

73
                    $message = sprintf('Could not find "%s": uses default configuration.', /** @scrutinizer ignore-type */ $input->getOption('config'));
Loading history...
74
                    $this->getBuilder()->getLogger()->warning($message);
75
                    $this->configFile = null;
76
                }
77
            }
78
        }
79
80
        parent::initialize($input, $output);
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function run(InputInterface $input, OutputInterface $output)
87
    {
88
        if ($output->isDebug()) {
89
            parent::run($input, $output);
90
        }
91
        // simplifying error message
92
        try {
93
            parent::run($input, $output);
94
        } catch (\Exception $e) {
95
            if ($this->io === null) {
96
                $this->io = new SymfonyStyle($input, $output);
97
            }
98
            $this->io->error($e->getMessage());
99
100
            return 1;
101
        }
102
    }
103
104
    /**
105
     * Returns the working directory.
106
     *
107
     * @return string|null
108
     */
109
    protected function getPath(): ?string
110
    {
111
        return $this->path;
112
    }
113
114
    /**
115
     * Returns the config file path.
116
     *
117
     * @return string|null
118
     */
119
    protected function getConfigFile(): ?string
120
    {
121
        return $this->configFile;
122
    }
123
124
    /**
125
     * Creates or returns a Builder instance.
126
     *
127
     * @param array $config
128
     *
129
     * @return Builder
130
     */
131
    protected function getBuilder(array $config = []): Builder
132
    {
133
        try {
134
            if (is_file($this->getConfigFile())) {
135
                $configContent = Util::fileGetContents($this->getConfigFile());
136
                if ($configContent === false) {
137
                    throw new \Exception('Can\'t read the configuration file.');
138
                }
139
                $siteConfig = Yaml::parse($configContent);
140
                $config = array_replace_recursive($siteConfig, $config);
141
            }
142
            $this->builder = (new Builder($config, new ConsoleLogger($this->output)))
143
                ->setSourceDir($this->getPath())
144
                ->setDestinationDir($this->getPath());
145
        } catch (ParseException $e) {
146
            throw new \Exception(sprintf('Configuration file parse error: %s', $e->getMessage()));
147
        } catch (\Exception $e) {
148
            throw new \Exception($e->getMessage());
149
        }
150
151
        return $this->builder;
152
    }
153
}
154