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 |
||
28 | class WhoDidItBehavior extends Behavior |
||
29 | { |
||
30 | |||
31 | /** |
||
32 | * Table which this behavior is attached to. |
||
33 | * |
||
34 | * @var \Cake\ORM\Table |
||
35 | */ |
||
36 | protected $_table; |
||
37 | |||
38 | /** |
||
39 | * Default configuration. |
||
40 | * |
||
41 | * - idCallable: It can be either a *callable* method that should return logged |
||
42 | * User's ID or a *string* representing a `session key` from where to read |
||
43 | * the ID. By Defaults it's set yo `Auth.id` for reading from Auth's session. |
||
44 | * |
||
45 | * - createdByField: The name of the "created_by" field in DB. Defaults to |
||
46 | * `created_by`. |
||
47 | * |
||
48 | * - modifiedByField: The name of the "modified_by" field in DB. Default to |
||
49 | * `modified_by`. |
||
50 | * |
||
51 | * - userModel: The name of the Users class table, used to bind user's |
||
52 | * information to the table being managed by this behavior. Defaults to |
||
53 | * `User.Users` |
||
54 | * |
||
55 | * - autoBind: Automatically bind the table to the User table. (default true) |
||
56 | * |
||
57 | * @var array |
||
58 | */ |
||
59 | protected $_defaultConfig = [ |
||
60 | 'idCallable' => 'Auth.id', |
||
61 | 'createdByField' => 'created_by', |
||
62 | 'modifiedByField' => 'modified_by', |
||
63 | 'userModel' => 'User.Users', |
||
64 | 'autoBind' => true, |
||
65 | ]; |
||
66 | |||
67 | /** |
||
68 | * Constructor. |
||
69 | * |
||
70 | * @param \Cake\ORM\Table $table The table this behavior is attached to |
||
71 | * @param array $config Configuration array for this behavior |
||
72 | */ |
||
73 | public function __construct(Table $table, array $config = []) |
||
96 | |||
97 | /** |
||
98 | * Run before a model is saved. |
||
99 | * |
||
100 | * @param \Cake\Event\Event $event The event that was triggered |
||
101 | * @param \Cake\ORM\Entity $entity The entity being saved |
||
102 | * @param array $options Array of options for the save operation |
||
103 | * @return bool True if save should proceed, false otherwise |
||
104 | */ |
||
105 | public function beforeSave(Event $event, $entity, $options = []) |
||
121 | |||
122 | /** |
||
123 | * Gets current User's ID. |
||
124 | * |
||
125 | * @return int User ID, zero if not found |
||
126 | */ |
||
127 | protected function _getUserId() |
||
141 | } |
||
142 |