Code Duplication    Length = 40-42 lines in 2 locations

src/Charcoal/Loader/FileLoader.php 1 location

@@ 230-271 (lines=42) @@
227
     * @throws InvalidArgumentException If a JSON decoding error occurs.
228
     * @return array|null
229
     */
230
    protected function loadJsonFile($filename)
231
    {
232
        $content = file_get_contents($filename);
233
234
        if ($content === null) {
235
            return null;
236
        }
237
238
        $data  = json_decode($content, true);
239
        $error = json_last_error();
240
241
        if ($error == JSON_ERROR_NONE) {
242
            return $data;
243
        }
244
245
        switch ($error) {
246
            case JSON_ERROR_NONE:
247
                break;
248
            case JSON_ERROR_DEPTH:
249
                $issue = 'Maximum stack depth exceeded';
250
                break;
251
            case JSON_ERROR_STATE_MISMATCH:
252
                $issue = 'Underflow or the modes mismatch';
253
                break;
254
            case JSON_ERROR_CTRL_CHAR:
255
                $issue = 'Unexpected control character found';
256
                break;
257
            case JSON_ERROR_SYNTAX:
258
                $issue = 'Syntax error, malformed JSON';
259
                break;
260
            case JSON_ERROR_UTF8:
261
                $issue = 'Malformed UTF-8 characters, possibly incorrectly encoded';
262
                break;
263
            default:
264
                $issue = 'Unknown error';
265
                break;
266
        }
267
268
        throw new InvalidArgumentException(
269
            sprintf('JSON %s could not be parsed: "%s"', $filename, $issue)
270
        );
271
    }
272
273
    /**
274
     * Retrieve the searchable paths.

src/Charcoal/Model/Service/MetadataLoader.php 1 location

@@ 462-501 (lines=40) @@
459
     * @throws InvalidArgumentException If a JSON decoding error occurs.
460
     * @return array|null
461
     */
462
    private function loadJsonFile($filename)
463
    {
464
        $content = file_get_contents($filename);
465
466
        if ($content === null) {
467
            return null;
468
        }
469
470
        $data  = json_decode($content, true);
471
        $error = json_last_error();
472
473
        if ($error == JSON_ERROR_NONE) {
474
            return $data;
475
        }
476
477
        $issue = 'Unknown error';
478
        switch ($error) {
479
            case JSON_ERROR_NONE:
480
                break;
481
            case JSON_ERROR_DEPTH:
482
                $issue = 'Maximum stack depth exceeded';
483
                break;
484
            case JSON_ERROR_STATE_MISMATCH:
485
                $issue = 'Underflow or the modes mismatch';
486
                break;
487
            case JSON_ERROR_CTRL_CHAR:
488
                $issue = 'Unexpected control character found';
489
                break;
490
            case JSON_ERROR_SYNTAX:
491
                $issue = 'Syntax error, malformed JSON';
492
                break;
493
            case JSON_ERROR_UTF8:
494
                $issue = 'Malformed UTF-8 characters, possibly incorrectly encoded';
495
                break;
496
        }
497
498
        throw new InvalidArgumentException(
499
            sprintf('JSON %s could not be parsed: "%s"', $filename, $issue)
500
        );
501
    }
502
503
    /**
504
     * Convert a snake-cased namespace to a file path.