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 |
||
19 | class LoaderTest extends \PHPUnit_Framework_TestCase |
||
20 | { |
||
21 | public $classMapFile = 'autoloader.classmap.php'; |
||
22 | |||
23 | public function setUp() |
||
41 | |||
42 | public function tearDown() |
||
46 | |||
47 | public static function apcClearCache() |
||
53 | |||
54 | /** |
||
55 | * testMethodautoload(). |
||
56 | */ |
||
57 | public function testMethodautoload() |
||
58 | { |
||
59 | // workflow of autoloading |
||
60 | |||
61 | // 1. existing class |
||
62 | $this->assertFalse(Loader::autoload('ThisClassExists')); |
||
63 | // 2. existing interface |
||
64 | $this->assertFalse(Loader::autoload('ThisInterfaceExists')); |
||
65 | // 3. existing trait |
||
66 | if (PHP_VERSION_ID <= 50400) { |
||
67 | $this->assertFalse(Loader::autoload('ClassADefinesTraitA')); |
||
68 | } |
||
69 | // PHP 5.4.6 Bug... trait_exists does not return anything (true|false|null). |
||
70 | // So a "cannot redeclare class TraitA" fatal error is thrown. |
||
71 | //$this->assertFalse(Loader::autoload('ClassBDefinesTraitA')); |
||
72 | |||
73 | // 1. autoloadExclusions() |
||
74 | // 2. autoloadInclusions() |
||
75 | // 3. autoloadByApcOrFileMap() |
||
76 | // 4. autoloadIncludePath() |
||
77 | // 5. autoloadTryPathsAndMap() |
||
78 | } |
||
79 | |||
80 | public function testMethodconstruct() |
||
81 | { |
||
82 | // unregister first (autoloader was registered during test setup) |
||
83 | $r = spl_autoload_unregister(['Koch\Autoload\Loader', 'autoload']); |
||
84 | |||
85 | // registers autoloader via constructor |
||
86 | new Loader(); |
||
87 | |||
88 | // test Koch Framework Autoloader is registered in the spl_autoloader_stack at first place |
||
89 | $registered_autoloaders = spl_autoload_functions(); |
||
90 | |||
91 | $this->assertTrue(is_string($registered_autoloaders[0][0])); |
||
92 | $this->assertFalse(is_object($registered_autoloaders[0][0])); |
||
93 | |||
94 | $this->assertEquals('Koch\Autoload\Loader', $registered_autoloaders[0][0]); |
||
95 | $this->assertEquals('autoload', $registered_autoloaders[0][1]); |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * testMethodautoloadExclusions(). |
||
100 | */ |
||
101 | public function testMethodautoloadExclusions() |
||
120 | |||
121 | /** |
||
122 | * testMethodautoloadInclusions(). |
||
123 | */ |
||
124 | public function testMethodautoloadInclusions() |
||
133 | |||
134 | /** |
||
135 | * testMethodautoloadByApcOrFileMap. |
||
136 | */ |
||
137 | public function testMethodautoloadByApcOrFileMap() |
||
145 | |||
146 | /** |
||
147 | * testMethodautoloadIncludePath(). |
||
148 | */ |
||
149 | public function testMethodautoloadIncludePath() |
||
150 | { |
||
151 | // try to load an unknown class |
||
152 | $this->assertFalse(Loader::autoloadIncludePath('\Namespace\Library\SomeUnknownClass')); |
||
153 | |||
154 | // set the include path to our fixtures directory, where a namespaced class exists |
||
155 | $path = __DIR__ . '/fixtures/Application'; |
||
156 | set_include_path($path . PATH_SEPARATOR . get_include_path()); |
||
157 | |||
158 | // try to load existing namespaced class |
||
159 | $this->assertTrue(Loader::autoloadIncludePath('NamespacedClass')); |
||
160 | } |
||
161 | |||
162 | View Code Duplication | public function testMethodwriteAutoloadingMapFile() |
|
163 | { |
||
164 | if (is_file($this->classMapFile)) { |
||
165 | unlink($this->classMapFile); |
||
166 | } |
||
167 | |||
168 | // file will be created |
||
169 | $this->assertSame([], Loader::readAutoloadingMapFile()); |
||
170 | $this->assertTrue(is_file($this->classMapFile)); |
||
171 | |||
172 | $array = ['class' => 'file']; |
||
173 | $this->assertTrue(Loader::writeAutoloadingMapFile($array)); |
||
174 | $this->assertSame($array, Loader::readAutoloadingMapFile()); |
||
175 | } |
||
176 | |||
177 | View Code Duplication | public function testMethodreadAutoloadingMapFile() |
|
178 | { |
||
179 | if (is_file($this->classMapFile)) { |
||
180 | unlink($this->classMapFile); |
||
181 | } |
||
182 | // file will be created |
||
183 | $this->assertSame([], Loader::readAutoloadingMapFile()); |
||
184 | $this->assertTrue(is_file($this->classMapFile)); |
||
185 | |||
186 | $array = ['class' => 'file']; |
||
187 | $this->assertTrue(Loader::writeAutoloadingMapFile($array)); |
||
188 | $this->assertSame($array, Loader::readAutoloadingMapFile()); |
||
189 | } |
||
190 | |||
191 | public function testMethodwriteAutoloadingMapApc() |
||
201 | |||
202 | public function testMethodreadAutoloadingMapApc() |
||
210 | |||
211 | View Code Duplication | public function testMethodaddMapping() |
|
233 | |||
234 | View Code Duplication | public function testMethodincludeFileAndMap() |
|
235 | { |
||
236 | $file = realpath(__DIR__ . '/fixtures/includeFileAndMap.php'); |
||
237 | $class = 'includeFileAndMapClass'; |
||
238 | |||
239 | Loader::includeFileAndMap($file, $class); |
||
240 | |||
241 | // test if the entry was added to the autoloader class map array |
||
242 | $map = Loader::getAutoloaderClassMap(); |
||
243 | |||
244 | $this->assertTrue(true, array_key_exists($class, $map)); |
||
245 | |||
246 | $this->assertEquals($map[$class], $file); |
||
247 | |||
248 | // file already loaded |
||
249 | $this->assertTrue(class_exists($class, false)); |
||
250 | } |
||
251 | |||
252 | public function testMethodincludeFile() |
||
266 | |||
267 | public function testsetInclusionMap() |
||
273 | } |
||
274 |
This check examines a number of code elements and verifies that they conform to the given naming conventions.
You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.