Completed
Push — master ( b63529...4d9787 )
by Julián
02:19
created

src/Message/GcmMessage.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Push notification services abstraction (http://github.com/juliangut/tify)
4
 *
5
 * @link https://github.com/juliangut/tify for the canonical source repository
6
 *
7
 * @license https://github.com/juliangut/tify/blob/master/LICENSE
8
 */
9
10
namespace Jgut\Tify\Message;
11
12
class GcmMessage extends AbstractMessage
13
{
14
    /**
15
     * List of Google service's reserved parameters.
16
     *
17
     * @var array
18
     */
19
    protected static $reservedParameters = [
20
        'from',
21
        'collapse_key',
22
        'delay_while_idle',
23
        'time_to_live',
24
        'restricted_package_name',
25
        'dry_run',
26
    ];
27
28
    /**
29
     * {@inheritdoc}
30
     *
31
     * @throws \InvalidArgumentException
32
     */
33
    public function setParameter($parameter, $value)
34
    {
35
        $parameter = trim($parameter);
36
37
        if (preg_match('/^(google|gcm)/', $parameter) || in_array($parameter, self::$reservedParameters)) {
38
            throw new \InvalidArgumentException(
39
                sprintf('"%s" can not be used as a custom parameter as it is reserved', $parameter)
40
            );
41
        }
42
43
        $this->parameters[$parameter] = $value;
44
45
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Jgut\Tify\Message\GcmMessage) is incompatible with the return type of the parent method Jgut\Tify\Message\AbstractMessage::setParameter of type Jgut\Tify\ParametersTrait.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
46
    }
47
}
48