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 |
||
24 | class TaskFactory |
||
25 | { |
||
26 | /** |
||
27 | * @var Runtime |
||
28 | */ |
||
29 | protected $runtime; |
||
30 | |||
31 | /** |
||
32 | * @var array Registered Tasks |
||
33 | */ |
||
34 | protected $registeredTasks = []; |
||
35 | |||
36 | /** |
||
37 | * Constructor |
||
38 | * |
||
39 | * @param Runtime $runtime |
||
40 | */ |
||
41 | public function __construct(Runtime $runtime) |
||
42 | { |
||
43 | $this->runtime = $runtime; |
||
44 | $this->loadBuiltInTasks(); |
||
45 | $this->loadCustomTasks($runtime->getConfigOption('custom_tasks', [])); |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * Add a Task |
||
50 | * |
||
51 | * @param AbstractTask $task |
||
52 | */ |
||
53 | public function add(AbstractTask $task) |
||
54 | { |
||
55 | $task->setRuntime($this->runtime); |
||
56 | $this->registeredTasks[$task->getName()] = $task; |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * Get a Task by it's registered Name/Code, or it can be a Class Name, |
||
61 | * in that case the class will be instantiated |
||
62 | * |
||
63 | * @param string $name Name/Code or Class of the Task |
||
64 | * @return AbstractTask |
||
65 | * @throws RuntimeException |
||
66 | */ |
||
67 | public function get($name) |
||
68 | { |
||
69 | $options = []; |
||
70 | if (is_array($name)) { |
||
71 | $options = $name; |
||
72 | list($name) = array_keys($name); |
||
73 | $options = $options[$name]; |
||
74 | } |
||
75 | |||
76 | if (array_key_exists($name, $this->registeredTasks)) { |
||
77 | /** @var AbstractTask $task */ |
||
78 | $task = $this->registeredTasks[$name]; |
||
79 | $task->setOptions($options); |
||
80 | return $task; |
||
81 | } elseif (class_exists($name)) { |
||
82 | $reflex = new ReflectionClass($name); |
||
83 | if ($reflex->isInstantiable()) { |
||
84 | $task = new $name(); |
||
85 | if ($task instanceof AbstractTask) { |
||
86 | $task->setOptions($options); |
||
87 | $this->add($task); |
||
88 | return $task; |
||
89 | } |
||
90 | } |
||
91 | } |
||
92 | |||
93 | throw new RuntimeException(sprintf('Invalid task name "%s"', $name)); |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Load BuiltIn Tasks |
||
98 | */ |
||
99 | View Code Duplication | protected function loadBuiltInTasks() |
|
|
|||
100 | { |
||
101 | $finder = new Finder(); |
||
102 | $finder->files()->in(__DIR__ . '/BuiltIn')->name('*Task.php'); |
||
103 | |||
104 | /** @var SplFileInfo $file */ |
||
105 | foreach ($finder as $file) { |
||
106 | $taskClass = substr('\\Mage\\Task\\BuiltIn\\' . str_replace('/', '\\', $file->getRelativePathname()), 0, -4); |
||
107 | if (class_exists($taskClass)) { |
||
108 | $reflex = new ReflectionClass($taskClass); |
||
109 | if ($reflex->isInstantiable()) { |
||
110 | $task = new $taskClass(); |
||
111 | if ($task instanceof AbstractTask) { |
||
112 | $this->add($task); |
||
113 | } |
||
114 | } |
||
115 | } |
||
116 | } |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * Load Custom Tasks |
||
121 | * @param array $tasksToLoad PreRegistered Tasks |
||
122 | * @throws RuntimeException |
||
123 | */ |
||
124 | protected function loadCustomTasks($tasksToLoad) |
||
145 | } |
||
146 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.