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 |
||
25 | class Logger extends AbstractLogger |
||
26 | { |
||
27 | const INFO = 'info'; |
||
28 | const ERROR = 'error'; |
||
29 | |||
30 | /** |
||
31 | * @var OutputInterface |
||
32 | */ |
||
33 | private $output; |
||
34 | |||
35 | /** |
||
36 | * @var array |
||
37 | */ |
||
38 | private $verbosityLevelMap = array( |
||
39 | LogLevel::EMERGENCY => OutputInterface::VERBOSITY_NORMAL, |
||
40 | LogLevel::ALERT => OutputInterface::VERBOSITY_NORMAL, |
||
41 | LogLevel::CRITICAL => OutputInterface::VERBOSITY_NORMAL, |
||
42 | LogLevel::ERROR => OutputInterface::VERBOSITY_NORMAL, |
||
43 | LogLevel::WARNING => OutputInterface::VERBOSITY_NORMAL, |
||
44 | LogLevel::NOTICE => OutputInterface::VERBOSITY_VERBOSE, |
||
45 | LogLevel::INFO => OutputInterface::VERBOSITY_VERY_VERBOSE, |
||
46 | LogLevel::DEBUG => OutputInterface::VERBOSITY_DEBUG, |
||
47 | ); |
||
48 | |||
49 | /** |
||
50 | * @var array |
||
51 | */ |
||
52 | private $formatLevelMap = array( |
||
53 | LogLevel::EMERGENCY => self::ERROR, |
||
54 | LogLevel::ALERT => self::ERROR, |
||
55 | LogLevel::CRITICAL => self::ERROR, |
||
56 | LogLevel::ERROR => self::ERROR, |
||
57 | LogLevel::WARNING => self::INFO, |
||
58 | LogLevel::NOTICE => self::INFO, |
||
59 | LogLevel::INFO => self::INFO, |
||
60 | LogLevel::DEBUG => self::INFO, |
||
61 | ); |
||
62 | |||
63 | /** |
||
64 | * ConsoleInterface constructor. |
||
65 | * |
||
66 | * @param OutputInterface $output |
||
67 | */ |
||
68 | 13 | public function __construct(OutputInterface $output) |
|
72 | |||
73 | /** |
||
74 | * Return the OutputInterface object. |
||
75 | * |
||
76 | * @return OutputInterface |
||
77 | */ |
||
78 | 13 | public function getOutputInterface() |
|
82 | |||
83 | /** |
||
84 | * Logs with an arbitrary level. |
||
85 | * |
||
86 | * @param mixed $level |
||
87 | * @param string $message |
||
88 | * @param array $context |
||
89 | */ |
||
90 | 13 | public function log($level, $message, array $context = array()) |
|
91 | { |
||
92 | 13 | if (!isset($this->verbosityLevelMap[$level])) |
|
93 | { |
||
94 | throw new InvalidArgumentException(sprintf('The log level "%s" does not exist.', $level)); |
||
95 | } |
||
96 | |||
97 | 13 | $verbosity = $this->output->getVerbosity(); |
|
98 | |||
99 | 13 | if ($verbosity >= $this->verbosityLevelMap[$level]) |
|
100 | { |
||
101 | 13 | $prefix = ''; |
|
102 | |||
103 | 13 | if ($verbosity >= OutputInterface::VERBOSITY_VERBOSE) |
|
104 | { |
||
105 | $prefix = sprintf('[%s] ', date('H:i:s')); |
||
106 | } |
||
107 | |||
108 | 13 | $this->output->writeln( |
|
109 | 13 | sprintf('<%1$s>%2$s[%3$s] %4$s</%1$s>', |
|
110 | 13 | $this->formatLevelMap[$level], |
|
111 | $prefix, |
||
112 | $level, |
||
113 | 13 | $this->interpolate($message, $context) |
|
114 | ) |
||
115 | ); |
||
116 | } |
||
117 | 13 | } |
|
118 | |||
119 | /** |
||
120 | * Writes a message to the output and adds a newline at the end. |
||
121 | * |
||
122 | * @param string|array $messages The message as an array of lines of a single string |
||
123 | * @param int $options A bitmask of options (one of the OUTPUT or VERBOSITY constants), 0 is considered |
||
124 | * the same as self::OUTPUT_NORMAL | self::VERBOSITY_NORMAL |
||
125 | */ |
||
126 | public function writeln($messages, $options = 0) |
||
130 | |||
131 | /** |
||
132 | * Interpolates context values into the message placeholders. |
||
133 | * |
||
134 | * @author PHP Framework Interoperability Group |
||
135 | * |
||
136 | * @param string $message |
||
137 | * @param array $context |
||
138 | * |
||
139 | * @return string |
||
140 | */ |
||
141 | 13 | View Code Duplication | private function interpolate($message, array $context) |
156 | } |
||
157 |
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.