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 |
||
22 | class SchemaLoader { |
||
23 | |||
24 | /** |
||
25 | * The cache contexts manager service. |
||
26 | * |
||
27 | * @var \Drupal\Core\Cache\Context\CacheContextsManager |
||
28 | */ |
||
29 | protected $contextsManager; |
||
30 | |||
31 | /** |
||
32 | * The schema plugin manager service. |
||
33 | * |
||
34 | * @var \Drupal\graphql\Plugin\GraphQL\SchemaPluginManager |
||
35 | */ |
||
36 | protected $schemaManager; |
||
37 | |||
38 | /** |
||
39 | * The schema cache backend. |
||
40 | * |
||
41 | * @var \Drupal\Core\Cache\CacheBackendInterface |
||
42 | */ |
||
43 | protected $schemaCache; |
||
44 | |||
45 | /** |
||
46 | * The cache metadata cache. |
||
47 | * |
||
48 | * @var \Drupal\Core\Cache\CacheBackendInterface |
||
49 | */ |
||
50 | protected $metadataCache; |
||
51 | |||
52 | /** |
||
53 | * The service configuration. |
||
54 | * |
||
55 | * @var array |
||
56 | */ |
||
57 | protected $config; |
||
58 | |||
59 | /** |
||
60 | * The request stack service. |
||
61 | * |
||
62 | * @var \Symfony\Component\HttpFoundation\RequestStack |
||
63 | */ |
||
64 | protected $requestStack; |
||
65 | |||
66 | /** |
||
67 | * Static cache of loaded schemas. |
||
68 | * |
||
69 | * @var \Youshido\GraphQL\Schema\AbstractSchema[] |
||
70 | */ |
||
71 | protected $schemas = []; |
||
72 | |||
73 | /** |
||
74 | * Static cache of loaded cache metadata. |
||
75 | * |
||
76 | * @var \Drupal\Core\Cache\RefinableCacheableDependencyInterface[] |
||
77 | */ |
||
78 | protected $metadata = []; |
||
79 | |||
80 | /** |
||
81 | * SchemaLoader constructor. |
||
82 | * |
||
83 | * @param \Drupal\Core\Cache\Context\CacheContextsManager $contextsManager |
||
84 | * The cache contexts manager service. |
||
85 | * @param \Symfony\Component\HttpFoundation\RequestStack $requestStack |
||
86 | * The request stack service. |
||
87 | * @param \Drupal\graphql\Plugin\GraphQL\SchemaPluginManager $schemaManager |
||
88 | * The schema plugin manager service. |
||
89 | * @param \Drupal\Core\Cache\CacheBackendInterface $schemaCache |
||
90 | * The schema cache backend. |
||
91 | * @param \Drupal\Core\Cache\CacheBackendInterface $metadataCache |
||
92 | * The metadata cache backend. |
||
93 | * @param array $config |
||
94 | * The configuration provided through the services.yml. |
||
95 | */ |
||
96 | public function __construct( |
||
111 | |||
112 | /** |
||
113 | * Loads and caches the generated schema. |
||
114 | * |
||
115 | * @param string $name |
||
116 | * The name of the schema to load. |
||
117 | * |
||
118 | * @return \Youshido\GraphQL\Schema\AbstractSchema |
||
119 | * The generated GraphQL schema. |
||
120 | */ |
||
121 | public function getSchema($name) { |
||
156 | |||
157 | /** |
||
158 | * Retrieves the schema's cache metadata. |
||
159 | * |
||
160 | * @param string $name |
||
161 | * The name of the schema. |
||
162 | * @return \Drupal\Core\Cache\CacheableDependencyInterface |
||
163 | * The cache metadata for the schema. |
||
164 | */ |
||
165 | public function getCacheMetadata($name) { |
||
195 | |||
196 | /** |
||
197 | * Collects schema cache metadata from all types registered with the schema. |
||
198 | * |
||
199 | * The cache metadata is statically cached. This means that the schema may not |
||
200 | * be modified after this method has been called. |
||
201 | * |
||
202 | * @param \Youshido\GraphQL\Schema\AbstractSchema $schema |
||
203 | * The schema to extract the cache metadata from. |
||
204 | * |
||
205 | * @return \Drupal\Core\Cache\CacheableMetadata |
||
206 | * The cache metadata collected from the schema's types. |
||
207 | */ |
||
208 | protected function extractCacheMetadata(AbstractSchema $schema) { |
||
228 | |||
229 | /** |
||
230 | * Maps a max age value to an "expire" value for the Cache API. |
||
231 | * |
||
232 | * @param int $maxAge |
||
233 | * A max age value. |
||
234 | * |
||
235 | * @return int |
||
236 | * A corresponding "expire" value. |
||
237 | * |
||
238 | * @see \Drupal\Core\Cache\CacheBackendInterface::set() |
||
239 | */ |
||
240 | View Code Duplication | protected function maxAgeToExpire($maxAge) { |
|
247 | |||
248 | /** |
||
249 | * Generates a cache identifier for the passed cache contexts. |
||
250 | * |
||
251 | * @param string $name |
||
252 | * The name of the schema. |
||
253 | * @param \Drupal\Core\Cache\CacheableDependencyInterface $metadata |
||
254 | * Optional array of cache context tokens. |
||
255 | * |
||
256 | * @return string The generated cache identifier. |
||
257 | * The generated cache identifier. |
||
258 | */ |
||
259 | protected function getCacheIdentifier($name, CacheableDependencyInterface $metadata) { |
||
264 | |||
265 | } |
||
266 |
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.