GenericServlet::init()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
/**
4
 * \AppserverIo\Psr\Servlet\GenericServlet
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2015 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/appserver-io-psr/servlet
18
 * @link      http://www.appserver.io
19
 */
20
21
namespace AppserverIo\Psr\Servlet;
22
23
/**
24
 * Abstract servlet implementation.
25
 *
26
 * @author    Tim Wagner <[email protected]>
27
 * @copyright 2015 TechDivision GmbH <[email protected]>
28
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
29
 * @link      https://github.com/appserver-io-psr/servlet
30
 * @link      http://www.appserver.io
31
 */
32
abstract class GenericServlet implements ServletInterface
33
{
34
35
    /**
36
     * The servlet configuration.
37
     *
38
     * @var \AppserverIo\Psr\Servlet\ServletConfigInterface
39
     */
40
    protected $servletConfig;
41
42
    /**
43
     * Information about the servlet, such as author, version, and copyright.
44
     *
45
     * @var string
46
     */
47
    protected $servletInfo = '';
48
49
    /**
50
     * Initializes the servlet with the passed configuration.
51
     *
52
     * @param \AppserverIo\Psr\Servlet\ServletConfigInterface $servletConfig The configuration to initialize the servlet with
53
     *
54
     * @return void
55
     */
56
    public function init(ServletConfigInterface $servletConfig)
57
    {
58
        $this->servletConfig = $servletConfig;
59
    }
60
61
    /**
62
     * Returns the servlet configuration.
63
     *
64
     * @return \AppserverIo\Psr\Servlet\ServletConfigInterface The servlet configuration
65
     */
66
    public function getServletConfig()
67
    {
68
        return $this->servletConfig;
69
    }
70
71
    /**
72
     * Returns information about the servlet, such as author, version, and copyright. By default, this method
73
     * returns an empty string. You have to override this method to have it return a meaningful value.
74
     *
75
     * @return string The servlet information
76
     */
77
    public function getServletInfo()
78
    {
79
        return $this->servletInfo;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->servletInfo; (string) is incompatible with the return type declared by the interface AppserverIo\Psr\Servlet\...terface::getServletInfo of type array.

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...
80
    }
81
82
    /**
83
     * Returns the servlet context instance.
84
     *
85
     * @return \AppserverIo\Psr\Servlet\ServletContextInterface The servlet context instance
86
     */
87
    public function getServletContext()
88
    {
89
        return $this->getServletConfig()->getServletContext();
90
    }
91
92
    /**
93
     * Returns the servlets name from the web.xml configuration file.
94
     *
95
     * @return string The servlet name
96
     */
97
    public function getServletName()
98
    {
99
        return $this->getServletConfig()->getServletName();
100
    }
101
102
    /**
103
     * Returns the init parameter with the passed name.
104
     *
105
     * @param string $name Name of the init parameter to return
106
     *
107
     * @return null|string
108
     */
109
    public function getInitParameter($name)
110
    {
111
        return $this->getServletConfig()->getInitParameter($name);
112
    }
113
}
114