Completed
Pull Request — master (#33)
by Pierre
07:29
created

ClassAnalyzer::canBeConstructed()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 18
rs 9.2
cc 4
eloc 9
nc 4
nop 1
1
<?php
2
3
namespace Knp\Rad\AutoRegistration\Reflection;
4
5
use ReflectionClass;
6
7
class ClassAnalyzer
8
{
9
    /**
10
     * @param string $class
11
     *
12
     * @return bool Return TRUE if class don't have constructor (or without required parameter) and if class is not abstract, FALSE else
13
     */
14
    public function canBeConstructed($class)
15
    {
16
        $class = $this->buildReflection($class);
17
18
        if ($class->isInterface()) {
19
            return false;
20
        }
21
22
        if ($class->isAbstract()) {
23
            return false;
24
        }
25
26
        if (null === $constuctor = $class->getConstructor()) {
27
            return true;
28
        }
29
30
        return 0 === $constuctor->getNumberOfRequiredParameters();
31
    }
32
33
    /**
34
     * @param string|ReflectionClass $class
35
     *
36
     * @return ReflectionClass
0 ignored issues
show
Documentation introduced by
Should the return type not be ReflectionClass|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
37
     */
38
    private function buildReflection($class)
39
    {
40
        if (true === $class instanceof ReflectionClass) {
41
            return $class;
42
        }
43
44
        if (true === class_exists($class)) {
45
            return new ReflectionClass($class);
46
        }
47
48
        return;
49
    }
50
}
51