Completed
Push — master ( 33953d...4a13d7 )
by Xeriab
03:37
created

Properties   C

Complexity

Total Complexity 73

Size/Duplication

Total Lines 437
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 73
c 1
b 1
f 0
lcom 1
cbo 4
dl 0
loc 437
ccs 11
cts 11
cp 1
rs 5.5447

10 Methods

Rating   Name   Duplication   Size   Complexity  
B parse() 0 28 2
A getSupportedFileExtensions() 0 4 1
F parseProperties() 0 145 34
F extractData() 0 98 21
A unescapeProperties() 0 8 2
A deleteFields() 0 10 3
A getProperties() 0 18 4
A loadFile() 0 13 4
A exportFormat() 0 6 1
A __toString() 0 4 1

How to fix   Complexity   

Complex Class

Complex classes like Properties often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Properties, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/**
4
 * Konfig.
5
 *
6
 * Yet another simple configuration loader library.
7
 *
8
 * PHP version 5
9
 *
10
 * @category Library
11
 * @package  Konfig
12
 * @author   Xeriab Nabil (aka KodeBurner) <[email protected]>
13
 * @license  https://raw.github.com/xeriab/konfig/master/LICENSE MIT
14
 * @link     https://xeriab.github.io/projects/konfig
15
 */
16
17
namespace Exen\Konfig\FileParser;
18
19
use Exception;
20
use Exen\Konfig\Arr;
21
use Exen\Konfig\Utils;
22
use Exen\Konfig\Exception\ParseException;
23
24
/**
25
 * Properties
26
 * Konfig's Java-Properties parser class.
27
 *
28
 * @category FileParser
29
 * @package  Konfig
30
 * @author   Xeriab Nabil (aka KodeBurner) <[email protected]>
31
 * @license  https://raw.github.com/xeriab/konfig/master/LICENSE MIT
32
 * @link     https://xeriab.github.io/projects/konfig
33
 *
34
 * @implements Exen\Konfig\FileParser\AbstractFileParser
35
 */
36
class Properties extends AbstractFileParser
37
{
38
    /**
39
     * Loads a PROPERTIES file as an array.
40
     *
41
     * @param string $path File path
42
     *
43
     * @throws ParseException If there is an error parsing PROPERTIES file
44
     *
45
     * @return array The parsed data
46
     *
47
     * @since 0.2.4
48
     */
49
    public function parse($path)
50
    {
51 6
        $data = null;
52
53 6
        $this->loadFile($path);
54
55 6
        // if (!$data || empty($data) || !is_array($data)) {
56
        //     throw new ParseException(
57 6
        //         [
58 3
        //         'message' => 'Error parsing PROPERTIES file',
59
        //         'file' => $path,
60 3
        //         ]
61 3
        //     );
62
        // }
63 2
64
        try {
65
            $data = $this->getProperties();
66 3
        } catch (Exception $ex) {
67
            throw new ParseException(
68
                [
69
                    'message' => 'Error parsing PROPERTIES file',
70
                    'exception' => $ex
71
                ]
72
            );
73
        }
74
75
        return $data;
76 3
    }
77
78 3
    /**
79
     * {@inheritdoc}
80
     *
81
     * @return array Supported extensions
82
     *
83
     * @since 0.1.0
84
     */
85
    public function getSupportedFileExtensions()
86
    {
87
        return ['properties'];
88
    }
89
90
    /**
91
     * Parse Java-Properties
92
     *
93
     * @return             array The parsed data
94
     * @since              0.2.6
95
     * @codeCoverageIgnore
96
     */
97
    private function parseProperties()
98
    {
99
        $analysis = [];
100
101
        static $isWaitingForOtherLine = false;
102
103
        foreach ($this->parsedFile as $lineNb => $line) {
0 ignored issues
show
Bug introduced by
The expression $this->parsedFile of type string is not traversable.
Loading history...
104
            $ifl = array_flip(array_keys($this->parsedFile))[$lineNb] + 1;
105
106
            if ((empty($line) || is_null($line)) && !$isWaitingForOtherLine) {
107
                $analysis[$lineNb] = ['emptyline', null, 'line' => $ifl];
108
                continue;
109
            }
110
111
            if (!$isWaitingForOtherLine && Utils::stringStart('#', $line)) {
112
                $analysis[$lineNb] = [
113
                    'comment',
114
                    trim(substr($line, 0)),
115
                    'line' => $ifl
116
                ];
117
118
                continue;
119
            }
120
121
            // Property name, check for escaped equal sign
122
            if (substr_count($line, '=') > substr_count($line, '\=')) {
123
                $temp = explode('=', $line, 2);
124
                $temp = Utils::trimArrayElements($temp);
125
126
                if (count($temp) === 2) {
127
                    $temp[1] = Utils::removeQuotes($temp[1]);
128
                    $analysis[$lineNb] = [
129
                        'property',
130
                        $temp[0],
131
                        $temp[1],
132
                        'line' => $ifl
133
                    ];
134
                }
135
136
                unset($temp);
137
138
                continue;
139
            } else {
140
                throw new Exception('Wrong Configuration');
141
                // break;
142
            }
143
144
            // Multiline data
145
            if (substr_count($line, '=') === 0) {
0 ignored issues
show
Unused Code introduced by
// Multiline data if (su... $ifl); continue; } does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
146
                $analysis[$lineNb] = ['multiline', null, $line, 'line' => $ifl];
147
                continue;
148
            }
149
        }
150
151
        // Second pass, we associate comments to entities
152
        $counter = Utils::getNumberLinesMatching('comment', $analysis);
153
154
        while ($counter > 0) {
155
            foreach ($analysis as $lineNb => $line) {
156
                if ($line[0] === 'comment'
157
                    && isset($analysis[$lineNb + 1][0])
158
                    && $analysis[$lineNb + 1][0] === 'comment'
159
                ) {
160
                    $analysis[$lineNb][1] .= ' ' . $analysis[$lineNb + 1][1];
161
                    $analysis[$lineNb + 1][0] = 'erase';
162
163
                    break;
164
                } elseif ($line[0] === 'comment'
165
                    && isset($analysis[$lineNb + 1][0])
166
                    && $analysis[$lineNb + 1][0] === 'property'
167
                ) {
168
                    // $analysis[$lineNb + 1][3] = $line[1];
169
                    $analysis[$lineNb][0] = 'erase';
170
                } elseif ($line[0] === 'comment'
171
                    && isset($analysis[$lineNb + 1][0])
172
                    && $analysis[$lineNb + 1][0] === 'emptyline'
173
                ) {
174
                    // $analysis[$lineNb + 1][3] = $line[1];
175
                    $analysis[$lineNb][0] = 'erase';
176
                }
177
            }
178
179
            $counter = Utils::getNumberLinesMatching('comment', $analysis);
180
            $analysis = $this->deleteFields('erase', $analysis);
181
        }
182
183
        // Count emptylines
184
        $counter = Utils::getNumberLinesMatching('emptyline', $analysis);
185
186
        while ($counter > 0) {
187
            foreach ($analysis as $lineNb => $line) {
188
                if ($line[0] === 'emptyline') {
189
                    $analysis[$lineNb][0] = 'erase';
190
                }
191
            }
192
193
            $counter = Utils::getNumberLinesMatching('emptyline', $analysis);
194
            $analysis = $this->deleteFields('erase', $analysis);
195
        }
196
197
        // Third pass, we merge multiline strings
198
199
        // We remove the backslashes at end of strings if they exist
200
        $analysis = Utils::stripBackslashes($analysis);
201
202
        // Count # of multilines
203
        $counter = Utils::getNumberLinesMatching('multiline', $analysis);
0 ignored issues
show
Bug introduced by
It seems like $analysis defined by \Exen\Konfig\Utils::stripBackslashes($analysis) on line 200 can also be of type null; however, Exen\Konfig\Utils::getNumberLinesMatching() does only seem to accept array, 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...
204
205
        while ($counter > 0) {
206
            foreach ($analysis as $lineNb => $line) {
0 ignored issues
show
Bug introduced by
The expression $analysis of type array|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
207
                if ($line[0] === 'multiline'
208
                    && isset($analysis[$lineNb - 1][0])
209
                    && $analysis[$lineNb - 1][0] === 'property'
210
                ) {
211
                    $analysis[$lineNb - 1][2] .= ' ' . trim($line[2]);
212
                    $analysis[$lineNb][0] = 'erase';
213
214
                    break;
215
                } elseif ($line[0] === 'multiline'
216
                    && isset($analysis[$lineNb - 1][0])
217
                    && $analysis[$lineNb - 1][0] === 'emptyline'
218
                ) {
219
                    // $analysis[$lineNb - 1][2] .= ' ' . trim($line[2]);
220
                    $analysis[$lineNb][0] = 'erase';
221
                }
222
            }
223
224
            $counter = Utils::getNumberLinesMatching('multiline', $analysis);
0 ignored issues
show
Bug introduced by
It seems like $analysis defined by \Exen\Konfig\Utils::stripBackslashes($analysis) on line 200 can also be of type null; however, Exen\Konfig\Utils::getNumberLinesMatching() does only seem to accept array, 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...
225
            $analysis = $this->deleteFields('erase', $analysis);
226
        }
227
228
        // Step 4, we clean up strings from escaped characters in properties
229
        $analysis = $this->unescapeProperties($analysis);
230
231
        // Step 5, we only have properties now, remove redondant field 0
232
        foreach ($analysis as $key => $value) {
0 ignored issues
show
Bug introduced by
The expression $analysis of type array|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
233
            if (preg_match('/^[1-9][0-9]*$/', $value[2])) {
234
                $value[2] = intval($value[2]);
235
            }
236
237
            array_splice($analysis[$key], 0, 1);
238
        }
239
240
        return $analysis;
241
    }
242
243
    /**
244
     * {@inheritdoc}
245
     *
246
     * @return array The exteacted data
247
     *
248
     * @since              0.2.4
249
     * @codeCoverageIgnore
250
     */
251
    public function extractData()
252
    {
253
        $analysis = [];
254
255
        // First pass, we categorize each line
256
        foreach ($this->parsedFile as $lineNb => $line) {
0 ignored issues
show
Bug introduced by
The expression $this->parsedFile of type string is not traversable.
Loading history...
257
            // Property name, check for escaped equal sign
258
            if (Utils::stringStart('#', $line)) {
259
                $analysis[$lineNb] = ['comment', trim(substr($line, 0))];
260
                continue;
261
            }
262
263
            // Property name, check for escaped equal sign
264
            if (substr_count($line, '=') > substr_count($line, '\=')) {
265
                $temp = explode('=', $line, 2);
266
                $temp = Utils::trimArrayElements($temp);
267
268
                if (count($temp) === 2) {
269
                    $temp[1] = Utils::removeQuotes($temp[1]);
270
                    $analysis[$lineNb] = ['property', $temp[0], $temp[1]];
271
                }
272
273
                unset($temp);
274
275
                continue;
276
            }
277
278
            // Multiline data
279
            if (substr_count($line, '=') === 0) {
280
                $analysis[$lineNb] = ['multiline', '', $line];
281
                continue;
282
            }
283
        }
284
285
        // Second pass, we associate comments to entities
286
        $counter = Utils::getNumberLinesMatching('comment', $analysis);
287
288
        while ($counter > 0) {
289
            foreach ($analysis as $lineNb => $line) {
290
                if ($line[0] === 'comment'
291
                    && isset($analysis[$lineNb + 1][0])
292
                    && $analysis[$lineNb + 1][0] === 'comment'
293
                ) {
294
                    $analysis[$lineNb][1] .= ' '.$analysis[$lineNb + 1][1];
295
                    $analysis[$lineNb + 1][0] = 'erase';
296
297
                    break;
298
                } elseif ($line[0] === 'comment'
299
                    && isset($analysis[$lineNb + 1][0])
300
                    && $analysis[$lineNb + 1][0] === 'property'
301
                ) {
302
                    $analysis[$lineNb + 1][3] = $line[1];
303
                    $analysis[$lineNb][0] = 'erase';
304
                }
305
            }
306
307
            $counter = Utils::getNumberLinesMatching('comment', $analysis);
308
            $analysis = $this->deleteFields('erase', $analysis);
309
        }
310
311
        // Third pass, we merge multiline strings
312
313
        // We remove the backslashes at end of strings if they exist
314
        $analysis = Utils::stripBackslashes($analysis);
315
316
        // Count # of multilines
317
        $counter = Utils::getNumberLinesMatching('multiline', $analysis);
0 ignored issues
show
Bug introduced by
It seems like $analysis defined by \Exen\Konfig\Utils::stripBackslashes($analysis) on line 314 can also be of type null; however, Exen\Konfig\Utils::getNumberLinesMatching() does only seem to accept array, 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...
318
319
        while ($counter > 0) {
320
            foreach ($analysis as $lineNb => $line) {
0 ignored issues
show
Bug introduced by
The expression $analysis of type array|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
321
                if ($line[0] === 'multiline'
322
                    && isset($analysis[$lineNb - 1][0])
323
                    && $analysis[$lineNb - 1][0] === 'property'
324
                ) {
325
                    $analysis[$lineNb - 1][2] .= ' ' . trim($line[2]);
326
                    $analysis[$lineNb][0] = 'erase';
327
                    break;
328
                }
329
            }
330
331
            $counter = Utils::getNumberLinesMatching('multiline', $analysis);
0 ignored issues
show
Bug introduced by
It seems like $analysis defined by \Exen\Konfig\Utils::stripBackslashes($analysis) on line 314 can also be of type null; however, Exen\Konfig\Utils::getNumberLinesMatching() does only seem to accept array, 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...
332
            $analysis = $this->deleteFields('erase', $analysis);
333
        }
334
335
        // Step 4, we clean up strings from escaped characters in properties
336
        $analysis = $this->unescapeProperties($analysis);
337
338
        // Step 5, we only have properties now, remove redondant field 0
339
        foreach ($analysis as $key => $value) {
0 ignored issues
show
Bug introduced by
The expression $analysis of type array|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
340
            if (preg_match('/^[1-9][0-9]*$/', $value[2])) {
341
                $value[2] = intval($value[2]);
342
            }
343
344
            array_splice($analysis[$key], 0, 1);
345
        }
346
347
        return $analysis;
348
    }
349
350
    /**
351
     * {@inheritdoc}
352
     *
353
     * @param array|null $analysis Configuration items
354
     *
355
     * @return array The configuration items
356
     *
357
     * @since              0.2.4
358
     * @codeCoverageIgnore
359
     */
360
    private function unescapeProperties($analysis)
361
    {
362
        foreach ($analysis as $key => $value) {
0 ignored issues
show
Bug introduced by
The expression $analysis of type array|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
363
            $analysis[$key][2] = str_replace('\=', '=', $value[2]);
364
        }
365
366
        return $analysis;
367
    }
368
369
    /**
370
     * {@inheritdoc}
371
     *
372
     * @param string     $field    Field name
373
     * @param array|null $analysis Configuration items
374
     *
375
     * @return array Configuration items after deletion
376
     *
377
     * @since              0.2.4
378
     * @codeCoverageIgnore
379
     */
380
    private function deleteFields($field, $analysis)
381
    {
382
        foreach ($analysis as $key => $value) {
0 ignored issues
show
Bug introduced by
The expression $analysis of type array|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
383
            if ($value[0] === $field) {
384
                unset($analysis[$key]);
385
            }
386
        }
387
388
        return array_values($analysis);
389
    }
390
391
    /**
392
     * {@inheritdoc}
393
     *
394
     * @param string|bool|null $file File path
395
     *
396
     * @return array Configuration items
397
     *
398
     * @since              0.2.4
399
     * @codeCoverageIgnore
400
     */
401
    public function getProperties($file = null)
402
    {
403
        if ($file && !is_null($file)) {
404
            $this->loadFile($file);
405
        }
406
407
        // $source = $this->extractData();
408
        $source = $this->parseProperties();
409
        $data = [];
410
411
        foreach ($source as $value) {
0 ignored issues
show
Bug introduced by
The expression $source of type array|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
412
            Arr::set($data, $value[0], $value[1]);
413
        }
414
415
        unset($this->parsedFile);
416
417
        return $data;
418
    }
419
420
    /**
421
     * Loads in the given file and parses it.
422
     *
423
     * @param string|bool|null $file File to load
424
     *
425
     * @return array The parsed file data
426
     *
427
     * @since              0.2.4
428
     * @codeCoverageIgnore
429
     */
430
    protected function loadFile($file = null)
431
    {
432
        $this->file = is_file($file) ? $file : false;
0 ignored issues
show
Documentation Bug introduced by
It seems like is_file($file) ? $file : false can also be of type boolean. However, the property $file 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...
433
434
        $contents = $this->parseVars(Utils::getContent($this->file));
0 ignored issues
show
Bug introduced by
It seems like $this->file can also be of type boolean; however, Exen\Konfig\Utils::getContent() does only seem to accept string|null, 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...
435
436
        if ($this->file && !is_null($file)) {
437
            $this->parsedFile = Utils::fileContentToArray($contents);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Exen\Konfig\Utils::fileContentToArray($contents) of type array is incompatible with the declared type string of property $parsedFile.

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...
438
            // $this->parsedFile = Utils::fileToArray($this->file);
439
        } else {
440
            $this->parsedFile = false;
0 ignored issues
show
Documentation Bug introduced by
The property $parsedFile was declared of type string, but false is of type false. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
441
        }
442
    }
443
444
    /**
445
     * Returns the formatted configuration file contents.
446
     *
447
     * @param array $contents configuration array
448
     *
449
     * @return string formatted configuration file contents
450
     *
451
     * @since              0.2.4
452
     * @codeCoverageIgnore
453
     */
454
    protected function exportFormat($contents = null)
455
    {
456
        throw new Exception(
457
            'Saving configuration to `Properties` is not supported at this time'
458
        );
459
    }
460
461
    /**
462
     * __toString.
463
     *
464
     * @return             string
465
     * @since              0.1.2
466
     * @codeCoverageIgnore
467
     */
468
    public function __toString()
469
    {
470
        return 'Exen\Konfig\FileParser\Properties' . PHP_EOL;
471
    }
472
}
473
474
// END OF ./src/FileParser/Properties.php FILE
475