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

phpDocumentor/Plugin/Scrybe/Converter/Factory.php (1 issue)

assigning incompatible types to properties.

Bug Documentation Major

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\Converter;
17
18
use Monolog\Logger;
19
use phpDocumentor\Plugin\Scrybe\Converter\Exception\ConverterNotFoundException;
20
21
/**
22
 * This factory attempts to create a converter given an input and output format and return that.
23
 *
24
 * <code>
25
 *     use phpDocumentor\Plugin\Scrybe\Converter\ConverterFactory;
26
 *     use phpDocumentor\Plugin\Scrybe\Converter\Format\Format;
27
 *
28
 *     $converter_factory = new ConverterFactory();
29
 *     $converter = $converter_factory->get(
30
 *         Format::MARKDOWN, Format::HTML
31
 *     );
32
 * <code>
33
 *
34
 * @author Mike van Riel <[email protected]>
35
 */
36
class Factory
37
{
38
    /** @var Definition\Factory */
39
    protected $definition_factory = null;
40
41
    /** @var ConverterInterface[] */
42
    protected $converters = [];
43
44
    /** @var Logger */
45
    protected $logger;
46
47
    /**
48
     * Constructs a new factory.
49
     *
50
     * A Definition\Factory may optionally be passed to provide an alternate method of creating Definitions or to
51
     * construct the Definition\Factory with a different Format\Collection to influence the possible options.
52
     *
53
     * @param string[] $converters
54
     */
55
    public function __construct(array $converters, Definition\Factory $definition_factory, Logger $logger)
56
    {
57
        if ($definition_factory === null) {
58
            $definition_factory = $this->getDefaultDefinitionFactory();
59
        }
60
61
        $this->converters = $converters;
0 ignored issues
show
Documentation Bug introduced by
It seems like $converters of type array<integer,string> is incompatible with the declared type array<integer,object<php...er\ConverterInterface>> of property $converters.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
62
        $this->setDefinitionFactory($definition_factory);
63
        $this->logger = $logger;
64
    }
65
66
    /**
67
     * Retrieves a new instance of the converter necessary to convert the give input format to the given output format.
68
     *
69
     * @throws ConverterNotFoundException
70
     */
71
    public function get(string $input_format, string $output_format): ConverterInterface
72
    {
73
        $definition = $this->definition_factory->get($input_format, $output_format);
74
75
        foreach ($this->converters as $class => $formats) {
76
            if ([$input_format, $output_format] === $formats) {
77
                $assets = new Metadata\Assets();
78
                $assets->setLogger($this->logger);
79
80
                $toc = new Metadata\TableOfContents();
81
                $glossary = new Metadata\Glossary();
82
83
                /** @var ConverterInterface $converter */
84
                $converter = new $class($definition, $assets, $toc, $glossary);
85
                $converter->setLogger($this->logger);
86
87
                return $converter;
88
            }
89
        }
90
91
        throw new ConverterNotFoundException(
92
            'No converter could be found to convert from ' . $input_format . ' to ' . $output_format
93
        );
94
    }
95
96
    /**
97
     * Returns a list of supported input formats for the given output format.
98
     *
99
     * @param string $given_output_format A format definition per the constants in the Format class.
100
     *
101
     * @return string[] An array of format definitions per the constantst in the Format class.
102
     */
103
    public function getSupportedInputFormats(string $given_output_format): array
104
    {
105
        $result = [];
106
        foreach ($this->converters as $formats) {
107
            list($input_format, $output_format) = $formats;
108
            if ($given_output_format === $output_format) {
109
                $result[] = $input_format;
110
            }
111
        }
112
113
        return $result;
114
    }
115
116
    /**
117
     * Sets the converters for this Factory.
118
     *
119
     * @param ConverterInterface[] $converters
120
     */
121
    public function setConverters(array $converters): void
122
    {
123
        $this->converters = $converters;
124
    }
125
126
    /**
127
     * Method used to retrieve the default Definition Factory.
128
     *
129
     * This is used when the user has not provided their own definition factory in the constructor.
130
     *
131
     * @see __construct() where this method is used.
132
     */
133
    protected function getDefaultDefinitionFactory(): Definition\Factory
134
    {
135
        return new Definition\Factory(new Format\Collection());
136
    }
137
138
    /**
139
     * Sets the Definition Factory used to retrieve definitions from.
140
     */
141
    protected function setDefinitionFactory(Definition\Factory $definition_factory): void
142
    {
143
        $this->definition_factory = $definition_factory;
144
    }
145
}
146