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 |
||
20 | trait FindTrait |
||
21 | { |
||
22 | /** |
||
23 | * Find multiple records based on provided query. |
||
24 | * |
||
25 | * Example: |
||
26 | * User::find(['status' => 'active'], ['profile']); |
||
27 | * |
||
28 | * @param array|\Closure $where Selection WHERE statement. |
||
29 | * @return DocumentSelector |
||
30 | */ |
||
31 | public static function find($where = []) |
||
35 | |||
36 | /** |
||
37 | * Fetch one record based on provided query or return null. Use second argument to specify |
||
38 | * relations to be loaded. |
||
39 | * |
||
40 | * Example: |
||
41 | * User::findOne(['name' => 'Wolfy-J'], ['profile'], ['id' => 'DESC']); |
||
42 | * |
||
43 | * @param array|\Closure $where Selection WHERE statement. |
||
44 | * @param array $sortBy Sort by. |
||
45 | * @return RecordEntity|null |
||
46 | */ |
||
47 | public static function findOne($where = [], array $sortBy = []) |
||
51 | |||
52 | /** |
||
53 | * Find record using it's primary key. Relation data can be preloaded with found record. |
||
54 | * |
||
55 | * Example: |
||
56 | * User::findByID(1, ['profile']); |
||
57 | * |
||
58 | * @param mixed $primaryKey Primary key. |
||
59 | * @return Document|null |
||
60 | */ |
||
61 | public static function findByPK($primaryKey) |
||
65 | |||
66 | /** |
||
67 | * Instance of ORM Selector associated with specific document. |
||
68 | * |
||
69 | * @see Component::staticContainer() |
||
70 | * @param ODM $odm ODM component, global container will be called if not instance provided. |
||
71 | * @return DocumentSource |
||
72 | * @throws ORMException |
||
73 | */ |
||
74 | View Code Duplication | public static function source(ODM $odm = null) |
|
88 | } |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.