Completed
Push — master ( 9d7197...6b758b )
by Rudie
02:04
created

IMAPMessageContent::subtypePart()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.2
cc 4
eloc 8
nc 6
nop 2
1
<?php
2
3
namespace rdx\imap;
4
5
abstract class IMAPMessageContent {
6
7
	protected $structure; // stdClass
8
	protected $parts = []; // Array<rdx\imap\IMAPMessagePart>
9
	protected $parameters = [];
10
11
	public function allParts( $withContainers = false ) {
12
		$parts = [];
13
		$iterate = function($message) use (&$iterate, &$parts, $withContainers) {
14
			foreach ( $message->parts() as $part ) {
15
				if ( $part->parts() ) {
16
					if ( $withContainers ) {
17
						$parts[] = $part;
18
					}
19
20
					$iterate($part);
21
				}
22
				else {
23
					$parts[] = $part;
24
				}
25
			}
26
		};
27
28
		$iterate($this);
29
		return $parts;
30
	}
31
32
	public function part( $index ) {
33
		$parts = $this->parts();
34
		return @$parts[$index];
35
	}
36
37
	public function parameters() {
38
		if ( empty($this->parameters) ) {
39
			$structure = $this->structure();
40
41
			$this->parameters['bytes'] = @$structure->bytes;
42
43
			foreach ((array) @$structure->parameters as $param) {
44
				$this->parameters[ strtolower($param->attribute) ] = $param->value;
45
			}
46
			foreach ((array) @$structure->dparameters as $param) {
47
				$this->parameters[ strtolower($param->attribute) ] = $param->value;
48
			}
49
		}
50
51
		return $this->parameters;
52
	}
53
54
	public function parameter( $name ) {
55
		$parameters = $this->parameters();
56
		return @$parameters[ strtolower($name) ];
57
	}
58
59
	public function subtypePart( $subtypes, $recursive ) {
60
		$subtypes = (array) $subtypes;
61
		$method = [$this, $recursive ? 'allParts' : 'parts'];
62
		$parts = call_user_func($method);
63
		array_unshift($parts, $this);
64
65
		foreach ( $parts as $part ) {
66
			if ( in_array($part->subtype(), $subtypes) ) {
67
				return $part;
68
			}
69
		}
70
	}
71
72
	public function subtypeContent( $subtypes, $recursive ) {
73
		if ( $part = $this->subtypePart($subtypes, $recursive) ) {
74
			return $part->content();
75
		}
76
	}
77
78
	public function text( $recursive = false ) {
79
		return $this->subtypeContent($this->mailbox()->getTextSubtypes(), $recursive);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class rdx\imap\IMAPMessageContent as the method mailbox() does only exist in the following sub-classes of rdx\imap\IMAPMessageContent: rdx\imap\IMAPMessage. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
80
	}
81
82
	public function html( $recursive = false ) {
83
		return $this->subtypeContent($this->mailbox()->getHtmlSubtypes(), $recursive);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class rdx\imap\IMAPMessageContent as the method mailbox() does only exist in the following sub-classes of rdx\imap\IMAPMessageContent: rdx\imap\IMAPMessage. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
84
	}
85
86
	public function deliveryStatus( $recursive = true ) {
87
		return $this->subtypeContent('DELIVERY-STATUS', $recursive);
88
	}
89
90
}
91