Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
14 | class Config { |
||
15 | /** |
||
16 | * Global Elgg configuration |
||
17 | * |
||
18 | * @var \stdClass |
||
19 | */ |
||
20 | private $CONFIG; |
||
21 | |||
22 | /** |
||
23 | * Constructor |
||
24 | */ |
||
25 | 2 | public function __construct() { |
|
29 | |||
30 | /** |
||
31 | * Get the URL for the current (or specified) site |
||
32 | * |
||
33 | * @param int $site_guid The GUID of the site whose URL we want to grab |
||
34 | * @return string |
||
35 | */ |
||
36 | 30 | function getSiteUrl($site_guid = 0) { |
|
51 | |||
52 | /** |
||
53 | * Get the plugin path for this installation |
||
54 | * |
||
55 | * @return string |
||
56 | */ |
||
57 | function getPluginsPath() { |
||
61 | |||
62 | /** |
||
63 | * Get the data directory path for this installation |
||
64 | * |
||
65 | * @return string |
||
66 | */ |
||
67 | function getDataPath() { |
||
71 | |||
72 | /** |
||
73 | * Get the root directory path for this installation |
||
74 | * |
||
75 | * @return string |
||
76 | */ |
||
77 | function getRootPath() { |
||
81 | |||
82 | /** |
||
83 | * Get an Elgg configuration value |
||
84 | * |
||
85 | * @param string $name Name of the configuration value |
||
86 | * @param int $site_guid null for installation setting, 0 for default site |
||
87 | * |
||
88 | * @return mixed Configuration value or null if it does not exist |
||
89 | */ |
||
90 | 1 | function get($name, $site_guid = 0) { |
|
128 | |||
129 | /** |
||
130 | * Set an Elgg configuration value |
||
131 | * |
||
132 | * @warning This does not persist the configuration setting. Use elgg_save_config() |
||
133 | * |
||
134 | * @param string $name Name of the configuration value |
||
135 | * @param mixed $value Value |
||
136 | * |
||
137 | * @return void |
||
138 | */ |
||
139 | function set($name, $value) { |
||
146 | |||
147 | /** |
||
148 | * Save a configuration setting |
||
149 | * |
||
150 | * @param string $name Configuration name (cannot be greater than 255 characters) |
||
151 | * @param mixed $value Configuration value. Should be string for installation setting |
||
152 | * @param int $site_guid null for installation setting, 0 for default site |
||
153 | * |
||
154 | * @return bool |
||
155 | */ |
||
156 | function save($name, $value, $site_guid = 0) { |
||
184 | } |
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:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.