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

Plugin/Scrybe/Command/Manual/ToLatexCommand.php (1 issue)

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\Format;
19
use phpDocumentor\Plugin\Scrybe\Converter\ToLatexInterface;
20
use Symfony\Component\Console\Input\InputInterface;
21
use Symfony\Component\Console\Input\InputOption;
22
23
/**
24
 * Command used to tell the application to convert from a format to Latex.
25
 */
26
class ToLatexCommand extends BaseConvertCommand
27
{
28
    /** @var string The string representation of the output format */
29
    protected $output_format = Format\Format::LATEX;
30
31
    /**
32
     * Defines the name, description and additional options for this command
33
     * and inherits the behaviour of the parent configure.
34
     *
35
     * @see ConvertCommandAbstract::configure() for the common business rules.
36
     */
37
    protected function configure()
38
    {
39
        parent::configure();
40
41
        $this->setName('manual:to-latex');
42
        $this->setDescription(
43
            'Generates reference documentation as a single Latex file'
44
        );
45
46
        $this->addOption(
47
            'author',
48
            null,
49
            InputOption::VALUE_OPTIONAL,
50
            'Name of the author'
51
        );
52
        $this->addOption(
53
            'cover-logo',
54
            null,
55
            InputOption::VALUE_OPTIONAL,
56
            'Path to a cover logo relative to the source root'
57
        );
58
        $this->addOption(
59
            'toc',
60
            null,
61
            InputOption::VALUE_OPTIONAL,
62
            'Whether the document should have a table of contents',
63
            true
64
        );
65
    }
66
67
    /**
68
     * Returns and configures the converter for this operation.
69
     *
70
     * This method overrides the parent getConverter method and invokes the
71
     * configureConverterFromInputOptions() method to set the options coming
72
     * from the Input object.
73
     *
74
     * @see BaseConvertCommand::getConverter() for the basic functionality.
75
     * @return phpDocumentor\Plugin\Scrybe\Converter\ConverterInterface
76
     */
77
    protected function getConverter(InputInterface $input)
78
    {
79
        /** @var ToLatexInterface $converter */
80
        $converter = parent::getConverter($input);
81
        $this->configureConverterFromInputOptions($converter, $input);
82
83
        return $converter;
0 ignored issues
show
Bug Best Practice introduced by Mike van Riel
The return type of return $converter; (phpDocumentor\Plugin\Scr...verter\ToLatexInterface) is incompatible with the return type of the parent method phpDocumentor\Plugin\Scr...rtCommand::getConverter of type phpDocumentor\Plugin\Scr...rter\ConverterInterface.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
84
    }
85
86
    /**
87
     * Configures the converter with the options provided by the Input options.
88
     *
89
     * @param ToLatexInterface $converter
90
     * @param InputInterface   $input
91
     *
92
     * @throws \InvalidArgumentException if the provided converter is not derived
93
     *     from ToLatexInterface
94
     */
95
    protected function configureConverterFromInputOptions($converter, $input)
96
    {
97
        if (!$converter instanceof ToLatexInterface) {
98
            throw new \InvalidArgumentException(
99
                'The converter used to process '
100
                . $input->getOption('input-format') . ' should implement the '
101
                . 'phpDocumentor\Plugin\Scrybe\Converter\ToPdfInterface'
102
            );
103
        }
104
105
        $converter->setTitle($input->getOption('title'));
106
        $converter->setAuthor($input->getOption('author'));
107
        $converter->setCoverLogo($input->getOption('cover-logo'));
108
        $converter->setTableOfContents($input->getOption('toc') !== 'false');
109
    }
110
}
111