Passed
Push — 1.0.0 ( fcaf32...d3c3e6 )
by Zaahid
03:21
created

MessagePartFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 20%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 0
dl 0
loc 54
ccs 3
cts 15
cp 0.2
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getInstance() 0 18 3
newInstance() 0 1 ?
1
<?php
2
/**
3
 * This file is part of the ZBateson\MailMimeParser project.
4
 *
5
 * @license http://opensource.org/licenses/bsd-license.php BSD
6
 */
7
namespace ZBateson\MailMimeParser\Message\Part;
8
9
use ZBateson\MailMimeParser\Header\HeaderFactory;
10
use ZBateson\MailMimeParser\Message\PartFilterFactory;
11
use ReflectionClass;
12
13
/**
14
 * Abstract factory for subclasses of MessagePart.
15
 *
16
 * @author Zaahid Bateson
17
 */
18
abstract class MessagePartFactory
19
{
20
    /**
21
     * @var PartStreamFilterManagerFactory responsible for creating
22
     *      PartStreamFilterManager instances
23
     */
24
    protected $partStreamFilterManagerFactory;
25
    
26
    /**
27
     * Initializes class dependencies.
28
     * 
29
     * @param PartStreamFilterManagerFactory $psf
30
     */
31 3
    public function __construct(PartStreamFilterManagerFactory $psf)
32
    {
33 3
        $this->partStreamFilterManagerFactory = $psf;
34 3
    }
35
    
36
    /**
37
     * Returns the singleton instance for the class.
38
     * 
39
     * @param PartStreamFilterManagerFactory $psf
40
     * @param HeaderFactory $hf
41
     * @param PartFilterFactory $pf
42
     * @return MessagePartFactory
43
     */
44
    public static function getInstance(
45
        PartStreamFilterManagerFactory $psf,
46
        HeaderFactory $hf = null,
47
        PartFilterFactory $pf = null
48
    ) {
49
        static $instances = [];
50
        $class = get_called_class();
51
        if (!isset($instances[$class])) {
52
            $rf = new ReflectionClass($class);
53
            $constr = $rf->getConstructor();
54
            if ($constr->getNumberOfParameters() === 3) {
55
                $instances[$class] = new static($psf, $hf, $pf);
0 ignored issues
show
Unused Code introduced by
The call to MessagePartFactory::__construct() has too many arguments starting with $hf.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
56
            } else {
57
                $instances[$class] = new static($psf);
58
            }
59
        }
60
        return $instances[$class];
61
    }
62
    
63
    /**
64
     * Constructs a new MessagePart object and returns it
65
     * 
66
     * @param string $messageObjectId
67
     * @param PartBuilder $partBuilder
68
     * @return \ZBateson\MailMimeParser\Message\Part\MessagePart
69
     */
70
    public abstract function newInstance($messageObjectId, PartBuilder $partBuilder);
0 ignored issues
show
Coding Style introduced by
The abstract declaration must precede the visibility declaration
Loading history...
71
}
72