NamespaceFilter   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 27
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 27
loc 27
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 5 5 1
A getIterator() 8 8 3

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
 * This program is free software. It comes without any warranty, to
4
 * the extent permitted by applicable law. You can redistribute it
5
 * and/or modify it under the terms of the Do What The Fuck You Want
6
 * To Public License, Version 2, as published by Sam Hocevar. See
7
 * http://www.wtfpl.net/ for more details.
8
 */
9
10
declare(strict_types = 1);
11
12
namespace hanneskod\classtools\Iterator\Filter;
13
14
use hanneskod\classtools\Iterator\ClassIterator;
15
use hanneskod\classtools\Iterator\Filter;
16
use hanneskod\classtools\Name;
17
18
/**
19
 * Filter classes based on namespace
20
 *
21
 * @author Hannes Forsgård <[email protected]>
22
 */
23 View Code Duplication
final class NamespaceFilter extends ClassIterator implements Filter
0 ignored issues
show
Duplication introduced by
This class 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...
24
{
25
    use FilterTrait;
26
27
    /**
28
     * @var string
29
     */
30
    private $namespace;
31
32
    /**
33
     * Register namespace to filter on
34
     */
35
    public function __construct(string $namespace)
36
    {
37
        parent::__construct();
38
        $this->namespace = new Name((string)$namespace);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \hanneskod\classtool...me((string) $namespace) of type object<hanneskod\classtools\Name> is incompatible with the declared type string of property $namespace.

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...
39
    }
40
41
    public function getIterator(): iterable
42
    {
43
        foreach ($this->getBoundIterator() as $className => $reflectedClass) {
44
            if ((new Name($className))->inNamespace($this->namespace)) {
0 ignored issues
show
Documentation introduced by
$this->namespace is of type string, but the function expects a object<hanneskod\classtools\Name>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
45
                yield $className => $reflectedClass;
46
            }
47
        }
48
    }
49
}
50