Completed
Push — develop ( 8eb671...133594 )
by Mike
19:30 queued 09:24
created

Scrybe/Command/Manual/BaseConvertCommand.php (5 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * This file is part of phpDocumentor.
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @author    Mike van Riel <[email protected]>
11
 * @copyright 2010-2018 Mike van Riel / Naenius (http://www.naenius.com)
12
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
13
 * @link      http://phpdoc.org
14
 */
15
16
namespace phpDocumentor\Plugin\Scrybe\Command\Manual;
17
18
use phpDocumentor\Plugin\Scrybe\Converter\ConverterInterface;
19
use phpDocumentor\Plugin\Scrybe\Converter\Factory as ConverterFactory;
20
use phpDocumentor\Plugin\Scrybe\Converter\Format;
21
use phpDocumentor\Plugin\Scrybe\Template\Factory;
22
use phpDocumentor\Plugin\Scrybe\Template\TemplateInterface;
23
use Symfony\Component\Console\Command\Command;
24
use Symfony\Component\Console\Input\InputArgument;
25
use Symfony\Component\Console\Input\InputInterface;
26
use Symfony\Component\Console\Input\InputOption;
27
use Symfony\Component\Console\Output\OutputInterface;
28
29
/**
30
 * Abstract Command class containing the scaffolding for the subsequent converting commands.
31
 */
32
abstract class BaseConvertCommand extends Command
33
{
34
    /** @var ConverterFactory */
35
    protected $converterFactory;
36
37
    /** @var Factory */
38
    protected $templateFactory;
39
40
    /** @var string The string representation of the output format */
41
    protected $output_format = Format\Format::HTML;
42
43
    /**
44
     * Initializes this command with a template and converter factory.
45
     *
46
     * @param string $name
47
     */
48
    public function __construct($name, Factory $templateFactory, ConverterFactory $converterFactory)
49
    {
50
        parent::__construct($name);
51
52
        $this->templateFactory = $templateFactory;
53
        $this->converterFactory = $converterFactory;
54
    }
55
56
    /**
57
     * Configures the options and default help text.
58
     */
59
    protected function configure()
60
    {
61
        $this
62
            ->addOption(
63
                'target',
64
                't',
65
                InputOption::VALUE_OPTIONAL,
66
                'target location for output',
67
                'build'
68
            )
69
            ->addOption(
70
                'input-format',
71
                'i',
72
                InputOption::VALUE_OPTIONAL,
73
                'which input format does the documentation sources have?',
74
                'rst'
75
            )
76
            ->addOption(
77
                'title',
78
                null,
79
                InputOption::VALUE_OPTIONAL,
80
                'The title of this document',
81
                'Scrybe'
82
            )
83
            ->addOption(
84
                'template',
85
                null,
86
                InputOption::VALUE_OPTIONAL,
87
                'which template should be used to generate the documentation?',
88
                'default'
89
            )
90
            ->addArgument(
91
                'source',
92
                InputArgument::IS_ARRAY | InputArgument::REQUIRED,
93
                'One or more files or directories to fetch files from'
94
            );
95
96
        $this->setHelp(
97
            <<<DESCRIPTION
98
Generates reference documentation as {$this->output_format}.
99
100
You can define the type of files use as input using the <info>--input-format</info>
101
of <info>-i</info> option.
102
103
DESCRIPTION
104
        );
105
    }
106
107
    /**
108
     * Execute the transformation process to an output format as defined in the
109
     * $output_format class variable.
110
     *
111
     * @see BaseConvertCommand::$output_format to determine the output format.
112
     * @return int
113
     */
114
    protected function execute(InputInterface $input, OutputInterface $output)
115
    {
116
        $this->getHelper('phpdocumentor_logger')->connectOutputToLogging($output, $this);
0 ignored issues
show
The method connectOutputToLogging() does not seem to exist on object<Symfony\Component...Helper\HelperInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
117
118
        $converter = $this->getConverter($input);
119
        $converter->setOption('title', $input->getOption('title'));
120
121
        $output->writeln('Collect all documents');
122
        $files = $this->buildCollection(
123
            $input->getArgument('source'),
124
            $converter->getDefinition()->getInputFormat()->getExtensions()
125
        );
126
127
        $output->writeln('Converting documents');
128
        $files = $converter->convert($files, $this->getTemplate($input));
0 ignored issues
show
It seems like $files can also be of type null; however, phpDocumentor\Plugin\Scr...terInterface::convert() does only seem to accept object<phpDocumentor\Plu...rter\Format\Collection>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
129
130
        $output->writeln('Writing converted documents to disk');
131
        $this->writeToDisk($files, $input->getOption('target'));
132
133
        $output->writeln('Writing assets to disk');
134
        $converter->getAssets()->copyTo($input->getOption('target'));
135
    }
136
137
    /**
138
     * @param string[] $files
139
     * @param string $destination
140
     */
141
    protected function writeToDisk($files, $destination)
142
    {
143
        foreach ($files as $relative_path => $contents) {
144
            $full_path = $destination . '/' . $relative_path;
145
146
            $destination_folder = dirname($full_path);
147
            if (!file_exists($destination_folder)) {
148
                mkdir($destination_folder, 0777, true);
149
            }
150
151
            file_put_contents($full_path, $contents);
152
        }
153
    }
154
155
    /**
156
     * Returns a template object based off the human-readable template name.
157
     *
158
     * @return TemplateInterface
159
     */
160
    protected function getTemplate(InputInterface $input)
161
    {
162
        $template = $this->getTemplateFactory()->get('twig');
163
        $template->setName($input->getOption('template'));
164
165
        return $template;
166
    }
167
168
    /**
169
     * Returns the converter for this operation.
170
     *
171
     * @return ConverterInterface
172
     */
173
    protected function getConverter(InputInterface $input)
174
    {
175
        return $this->getConverterFactory()->get($input->getOption('input-format'), $this->output_format);
176
    }
177
178
    /**
179
     * Constructs a Fileset collection and returns that.
180
     *
181
     * TODO: implement this
182
     *
183
     * @param array $sources    List of source paths.
184
     * @param array $extensions List of extensions to scan for in directories.
185
     *
186
     * @return Collection
187
     */
188
    protected function buildCollection(array $sources, array $extensions)
0 ignored issues
show
The parameter $sources is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
The parameter $extensions is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
189
    {
190
//        $collection = new Collection();
0 ignored issues
show
Unused Code Comprehensibility introduced by Jaapio
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
191
//        $collection->setAllowedExtensions($extensions);
192
//        foreach ($sources as $path) {
193
//            if (is_dir($path)) {
194
//                $collection->addDirectory($path);
195
//                continue;
196
//            }
197
//
198
//            $collection->addFile($path);
199
//        }
200
//
201
//        return $collection;
202
    }
203
204
    /**
205
     * Returns a factory object that can return any Scrybe template.
206
     *
207
     * @return Factory
208
     */
209
    protected function getTemplateFactory()
210
    {
211
        return $this->templateFactory;
212
    }
213
214
    /**
215
     * Returns the factory for converters.
216
     *
217
     * @return ConverterFactory
218
     */
219
    public function getConverterFactory()
220
    {
221
        return $this->converterFactory;
222
    }
223
}
224