Completed
Push — master ( b3758e...d24b37 )
by Sébastien
04:19
created

DefaultClassLoader   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 39.13 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
dl 18
loc 46
c 0
b 0
f 0
ccs 17
cts 17
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getReportClassLoader() 0 5 1
A getClassLoader() 18 18 4

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
 * Jasper report integration for PHP
7
 *
8
 * @link      https://github.com/belgattitude/soluble-jasper
9
 * @author    Vanvelthem Sébastien
10
 * @copyright Copyright (c) 2017 Vanvelthem Sébastien
11
 * @license   MIT
12
 */
13
14
namespace Soluble\Jasper\Context;
15
16
use Soluble\Japha\Bridge\Adapter as BridgeAdapter;
17
use Soluble\Japha\Interfaces\JavaObject;
18
use Soluble\Jasper\Exception\InvalidArgumentException;
19
use Soluble\Jasper\Report;
20
21
class DefaultClassLoader
22
{
23
    /**
24
     * @var BridgeAdapter
25
     */
26
    private $ba;
27
28 13
    public function __construct(BridgeAdapter $ba)
29
    {
30 13
        $this->ba = $ba;
31 13
    }
32
33
    /**
34
     * @return JavaObject Java('java.lang.ClassLoader')
35
     */
36 1
    public function getReportClassLoader(Report $report): JavaObject
37
    {
38 1
        $reportPath = $report->getReportPath();
39
40 1
        return $this->getClassLoader([$reportPath]);
41
    }
42
43
    /**
44
     * @param string[] $paths           paths that will be added to the classLoader
45
     * @param bool     $checkPathExists default to true, will throw exception if path does not exists
46
     *
47
     * @return JavaObject Java('java.lang.ClassLoader')
48
     */
49 13 View Code Duplication
    public function getClassLoader(array $paths, bool $checkPathExists = true): JavaObject
50
    {
51 13
        $classLoaderPaths = [];
52 13
        foreach ($paths as $path) {
53 13
            if ($checkPathExists && !is_dir($path)) {
54 1
                throw new InvalidArgumentException(
55 1
                    sprintf(
56 1
                        'Cannot add path to classLoader, it does not exists: %s',
57 1
                        $path
58
                    )
59
                );
60
            }
61 12
            $classLoaderPaths[] = $this->ba->java('java.io.File', $path)->toUrl();
62
        }
63
64 12
        $classLoaderPaths = array_unique($classLoaderPaths);
65
66 12
        return $this->ba->java('java.net.URLClassLoader', $classLoaderPaths);
67
    }
68
}
69