Completed
Push — develop ( 2993ce...48a3ec )
by Mike
09:32
created

Transformation   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 276
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 72.31%

Importance

Changes 0
Metric Value
dl 0
loc 276
ccs 47
cts 65
cp 0.7231
rs 10
c 0
b 0
f 0
wmc 26
lcom 1
cbo 2

16 Methods

Rating   Name   Duplication   Size   Complexity  
A setQuery() 0 4 1
A __construct() 0 7 1
A getQuery() 0 4 1
A setWriter() 0 4 1
A getWriter() 0 4 1
A setSource() 0 4 1
A getSource() 0 4 1
B getSourceAsPath() 0 44 7
A setArtifact() 0 4 1
A getArtifact() 0 4 1
A setParameters() 0 4 1
A getParameters() 0 4 1
A getParameter() 0 11 3
A getParametersWithKey() 0 13 3
A setTransformer() 0 4 1
A getTransformer() 0 4 1
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\Transformer;
17
18
use phpDocumentor\Transformer\Template\Parameter;
19
20
/**
21
 * Class representing a single Transformation.
22
 */
23
class Transformation
24
{
25
    /**
26
     * @var string Reference to an object containing the business logic used to execute this transformation.
27
     */
28
    protected $writer = null;
29
30
    /**
31
     * @var string the location where the output should be sent to; the exact function differs per writer.
32
     */
33
    protected $artifact = '';
34
35
    /**
36
     * @var string the location where input for a writer should come from; the exact function differs per writer.
37
     */
38
    protected $source = '';
39
40
    /**
41
     * @var string a filter or other form of limitation on what information of the AST is used; the exact function
42
     *     differs per writer.
43
     */
44
    protected $query = '';
45
46
    /**
47
     * @var Transformer The object guiding the transformation process and having meta-data of it.
48
     */
49
    protected $transformer;
50
51
    /**
52
     * @var Parameter[] A series of parameters that can influence what the writer does; the exact function differs
53
     *     per writer.
54
     */
55
    protected $parameters = [];
56
57
    /**
58
     * Constructs a new Transformation object and populates the required parameters.
59
     *
60
     * @param string $query       What information to use as datasource for the writer's source.
61
     * @param string $writer      What type of transformation to apply (XSLT, PDF, Checkstyle etc).
62
     * @param string $source      Which template or type of source to use.
63
     * @param string $artifact    What is the filename of the result (relative to the generated root)
64
     */
65 1
    public function __construct($query, $writer, $source, $artifact)
66
    {
67 1
        $this->setQuery($query);
68 1
        $this->setWriter($writer);
69 1
        $this->setSource($source);
70 1
        $this->setArtifact($artifact);
71 1
    }
72
73
    /**
74
     * Sets the query.
75
     *
76
     * @param string $query Free-form string with writer-specific values.
77
     */
78 1
    public function setQuery($query)
79
    {
80 1
        $this->query = $query;
81 1
    }
82
83
    /**
84
     * Returns the set query.
85
     *
86
     * @return string
87
     */
88 1
    public function getQuery()
89
    {
90 1
        return $this->query;
91
    }
92
93
    /**
94
     * Sets the writer type and instantiates a writer.
95
     *
96
     * @param string $writer Name of writer to instantiate.
97
     */
98 1
    public function setWriter(string $writer): void
99
    {
100 1
        $this->writer = $writer;
101 1
    }
102
103
    /**
104
     * Returns an instantiated writer object.
105
     *
106
     * @return string
107
     */
108 1
    public function getWriter(): string
109
    {
110 1
        return $this->writer;
111
    }
112
113
    /**
114
     * Sets the source / type which the writer will use to generate artifacts from.
115
     *
116
     * @param string $source Free-form string with writer-specific meaning.
117
     */
118 1
    public function setSource($source)
119
    {
120 1
        $this->source = $source;
121 1
    }
122
123
    /**
124
     * Returns the name of the source / type used in the transformation process.
125
     *
126
     * @return string
127
     */
128 1
    public function getSource()
129
    {
130 1
        return $this->source;
131
    }
132
133
    /**
134
     * Returns the source as a path instead of a regular value.
135
     *
136
     * This method applies the following rules to the value of $source:
137
     *
138
     * 1. if the template_path parameter is set and that combined with the
139
     *    source gives an existing file; return that.
140
     * 2. if the value exists as a file (either relative to the current working
141
     *    directory or absolute), do a realpath and return it.
142
     * 3. Otherwise prepend it with the phpDocumentor data folder, if that does
143
     *    not exist: throw an exception
144
     *
145
     * @throws Exception if no valid file could be found.
146
     *
147
     * @return string
148
     */
149
    public function getSourceAsPath(): string
150
    {
151
        // externally loaded templates set this parameter so that template
152
        // resources may be placed in the same folder as the template.
153
        if ($this->getParameter('template_path') !== null) {
154
            $path = rtrim($this->getParameter('template_path')->getValue(), '/\\');
155
            if (file_exists($path . DIRECTORY_SEPARATOR . $this->source)) {
156
                return $path . DIRECTORY_SEPARATOR . $this->source;
157
            }
158
        }
159
160
        // counter a BC break that we introduced in 2.0 stable; we removed the notion of global assets
161
        // to be able to provide composer integration
162
        // TODO: remove in version 3.0
163
        if (strpos($this->source, 'templates/') !== 0) {
164
            $this->source = 'templates/abstract/' . $this->source;
165
            trigger_error(
166
                'Using shared assets in a template is deprecated and will be removed in version 3.0',
167
                E_USER_DEPRECATED
168
            );
169
        }
170
171
        // check whether the file exists in the phpDocumentor project directory.
172
        if (file_exists(__DIR__ . '/../../../' . $this->source)) {
173
            return __DIR__ . '/../../../' . $this->source;
174
        }
175
176
        // in case of a composer installation
177
        if (file_exists(__DIR__ . '/../../../../templates')) {
178
            return __DIR__ . '/../../../../' . $this->source;
179
        }
180
181
        // TODO: replace this as it breaks the component stuff
182
        // we should ditch the idea of a global set of files to fetch and have
183
        // a variable / injection for the global templates folder and inject
184
        // that here.
185
        $file = __DIR__ . '/../../../data/' . $this->source;
186
187
        if (!file_exists($file)) {
188
            throw new Exception('The source path does not exist: ' . $file);
189
        }
190
191
        return $file;
192
    }
193
194
    /**
195
     * Filename of the resulting artifact relative to the root.
196
     *
197
     * If the query results in a set of artifacts (multiple nodes / array);
198
     * then this string must contain an identifying variable as returned by the
199
     * writer.
200
     *
201
     * @param string $artifact Name of artifact to generate; usually a filepath.
202
     */
203 1
    public function setArtifact($artifact)
204
    {
205 1
        $this->artifact = $artifact;
206 1
    }
207
208
    /**
209
     * Returns the name of the artifact.
210
     *
211
     * @return string
212
     */
213 1
    public function getArtifact()
214
    {
215 1
        return $this->artifact;
216
    }
217
218
    /**
219
     * Sets an array of parameters (key => value).
220
     *
221
     * @param Parameter[] $parameters Associative multidimensional array containing
222
     *     parameters for the Writer.
223
     */
224 1
    public function setParameters(array $parameters)
225
    {
226 1
        $this->parameters = $parameters;
227 1
    }
228
229
    /**
230
     * Returns all parameters for this transformation.
231
     *
232
     * @return Parameter[]
233
     */
234 1
    public function getParameters()
235
    {
236 1
        return $this->parameters;
237
    }
238
239
    /**
240
     * Returns a specific parameter, or $default if none exists.
241
     *
242
     * @param string $name Name of the parameter to return.
243
     *
244
     * @return Parameter|null
245
     */
246 2
    public function getParameter($name): ?Parameter
247
    {
248
        /** @var Parameter $parameter */
249 2
        foreach ($this->parameters as $parameter) {
250 1
            if ($parameter->getKey() === $name) {
251 1
                return $parameter;
252
            }
253
        }
254
255 1
        return null;
256
    }
257
258
    /**
259
     * Returns a specific parameter, or $default if none exists.
260
     *
261
     * @param string $name Name of the parameter to return.
262
     *
263
     * @return Parameter[]
264
     */
265 2
    public function getParametersWithKey($name)
266
    {
267 2
        $parameters = [];
268
269
        /** @var Parameter $parameter */
270 2
        foreach ($this->parameters as $parameter) {
271 1
            if ($parameter->getKey() === $name) {
272 1
                $parameters[] = $parameter;
273
            }
274
        }
275
276 2
        return $parameters;
277
    }
278
279
    /**
280
     * Sets the transformer on this transformation.
281
     *
282
     * @param \phpDocumentor\Transformer\Transformer $transformer
283
     */
284 1
    public function setTransformer($transformer)
285
    {
286 1
        $this->transformer = $transformer;
287 1
    }
288
289
    /**
290
     * Returns the transformer for this transformation.
291
     *
292
     * @return \phpDocumentor\Transformer\Transformer
293
     */
294 1
    public function getTransformer()
295
    {
296 1
        return $this->transformer;
297
    }
298
}
299