Passed
Branch master (c435fb)
by Théo
02:10
created

InstalledPackagesScoper   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 40.91 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 18
loc 44
rs 10
c 0
b 0
f 0
ccs 17
cts 17
cp 1
wmc 5
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A scope() 18 18 2
A prefixLockPackages() 0 8 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the humbug/php-scoper package.
7
 *
8
 * Copyright (c) 2017 Théo FIDRY <[email protected]>,
9
 *                    Pádraic Brady <[email protected]>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace Humbug\PhpScoper\Scoper\Composer;
16
17
use Humbug\PhpScoper\Scoper;
18
19
final class InstalledPackagesScoper implements Scoper
20
{
21
    private static $filePattern = '/composer\/installed\.json/';
22
23
    private $decoratedScoper;
24
25 2
    public function __construct(Scoper $decoratedScoper)
26
    {
27 2
        $this->decoratedScoper = $decoratedScoper;
28 2
    }
29
30
    /**
31
     * Scopes PHP and JSON files related to Composer.
32
     *
33
     * {@inheritdoc}
34
     */
35 2 View Code Duplication
    public function scope(string $filePath, string $prefix, array $patchers, callable $globalWhitelister): string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
36
    {
37 2
        if (1 !== preg_match(self::$filePattern, $filePath)) {
38 1
            return $this->decoratedScoper->scope($filePath, $prefix, $patchers, $globalWhitelister);
39
        }
40
41 1
        $decodedJson = json_decode(
42 1
            file_get_contents($filePath),
43 1
            true
44
        );
45
46 1
        $decodedJson = $this->prefixLockPackages($decodedJson, $prefix);
47
48 1
        return json_encode(
49 1
            $decodedJson,
50 1
            JSON_PRETTY_PRINT
51
        );
52
    }
53
54 1
    private function prefixLockPackages(array $packages, string $prefix): array
55
    {
56 1
        foreach ($packages as $index => $package) {
57 1
            $packages[$index] = AutoloadPrefixer::prefixPackageAutoloads($package, $prefix);
58
        }
59
60 1
        return $packages;
61
    }
62
}
63