Test Failed
Branch 1.0.0 (84f469)
by Zaahid
05:36
created

MimePartFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 3
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
12
/**
13
 * Responsible for creating MimePart instances.
14
 *
15
 * @author Zaahid Bateson
16
 */
17
class MimePartFactory extends MessagePartFactory
18
{
19
    /**
20
     * @var \ZBateson\MailMimeParser\Header\HeaderFactory the HeaderFactory
21
     *      instance
22
     */
23
    protected $headerFactory;
24
    
25
    /**
26
     * @var \ZBateson\MailMimeParser\Header\HeaderFactory the PartFilterFactory
27
     *      instance
28
     */
29
    protected $partFilterFactory;
30
31
    /**
32
     * Creates a MimePartFactory instance with its dependencies.
33
     * 
34
     * @param PartStreamFilterManagerFactory $psf
35
     * @param HeaderFactory $hf
36
     * @param PartFilterFactory $pf
37
     */
38
    public function __construct(PartStreamFilterManagerFactory $psf, HeaderFactory $hf, PartFilterFactory $pf)
39
    {
40
        parent::__construct($psf);
41
        $this->headerFactory = $hf;
42
        $this->partFilterFactory = $pf;
0 ignored issues
show
Documentation Bug introduced by
It seems like $pf of type object<ZBateson\MailMime...sage\PartFilterFactory> is incompatible with the declared type object<ZBateson\MailMime...r\Header\HeaderFactory> of property $partFilterFactory.

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...
43
    }
44
    
45
    /**
46
     * Returns the singleton instance for the class.
47
     * 
48
     * @param PartStreamFilterManagerFactory $psf
49
     * @param HeaderFactory $hf
50
     * @param PartFilterFactory $pf
51
     * @return MimePartFactory
52
     */
53 View Code Duplication
    public static function getInstance(
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...
54
        PartStreamFilterManagerFactory $psf,
55
        HeaderFactory $hf = null,
56
        PartFilterFactory $pf = null
57
    ) {
58
        static $instances = [];
59
        $class = get_called_class();
60
        if (!isset($instances[$class])) {
61
            $instances[$class] = new static($psf, $hf, $pf);
0 ignored issues
show
Bug introduced by
It seems like $hf defined by parameter $hf on line 55 can be null; however, ZBateson\MailMimeParser\...tFactory::__construct() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
Bug introduced by
It seems like $pf defined by parameter $pf on line 56 can be null; however, ZBateson\MailMimeParser\...tFactory::__construct() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
62
        }
63
        return $instances[$class];
64
    }
65
66
    /**
67
     * Constructs a new MimePart object and returns it
68
     * 
69
     * @param string $messageObjectId
70
     * @param PartBuilder $partBuilder
71
     * @return \ZBateson\MailMimeParser\Message\Part\MimePart
72
     */
73
    public function newInstance($messageObjectId, PartBuilder $partBuilder)
74
    {
75
        return new MimePart(
76
            $this->headerFactory,
77
            $this->partFilterFactory,
0 ignored issues
show
Documentation introduced by
$this->partFilterFactory is of type object<ZBateson\MailMime...r\Header\HeaderFactory>, but the function expects a object<ZBateson\MailMime...sage\PartFilterFactory>.

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...
78
            $messageObjectId,
79
            $partBuilder,
80
            $this->partStreamFilterManagerFactory->newInstance()
81
        );
82
    }
83
}
84