These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | /* |
||
4 | * This file is part of the overtrue/pinyin. |
||
5 | * |
||
6 | * (c) overtrue <[email protected]> |
||
7 | * |
||
8 | * This source file is subject to the MIT license that is bundled |
||
9 | * with this source code in the file LICENSE. |
||
10 | */ |
||
11 | |||
12 | namespace Overtrue\Pinyin; |
||
13 | |||
14 | use Closure; |
||
15 | |||
16 | /** |
||
17 | * Dict File loader. |
||
18 | */ |
||
19 | class FileDictLoader implements DictLoaderInterface |
||
20 | { |
||
21 | /** |
||
22 | * Words segment name. |
||
23 | * |
||
24 | * @var string |
||
25 | */ |
||
26 | protected $segmentName = 'words_%s'; |
||
27 | |||
28 | /** |
||
29 | * Dict path. |
||
30 | * |
||
31 | * @var string |
||
32 | */ |
||
33 | protected $path; |
||
34 | |||
35 | /** |
||
36 | * Constructor. |
||
37 | 10 | * |
|
38 | * @param string $path |
||
39 | 10 | */ |
|
40 | 10 | public function __construct($path) |
|
41 | { |
||
42 | $this->path = $path; |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * Load dict. |
||
47 | 10 | * |
|
48 | * @param Closure $callback |
||
49 | 10 | */ |
|
50 | 10 | View Code Duplication | public function map(Closure $callback) |
0 ignored issues
–
show
|
|||
51 | { |
||
52 | 10 | for ($i = 0; $i < 100; ++$i) { |
|
53 | 10 | $segment = $this->path.'/'.sprintf($this->segmentName, $i); |
|
54 | 10 | ||
55 | 10 | if (file_exists($segment)) { |
|
56 | 10 | $dictionary = (array) include $segment; |
|
57 | 10 | $callback($dictionary); |
|
58 | } |
||
59 | } |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * Load surname dict. |
||
64 | 1 | * |
|
65 | * @param Closure $callback |
||
66 | 1 | */ |
|
67 | public function mapSurname(Closure $callback) |
||
68 | 1 | { |
|
69 | 1 | $surnames = $this->path.'/surnames'; |
|
70 | 1 | ||
71 | 1 | if (file_exists($surnames)) { |
|
72 | 1 | $dictionary = (array) include $surnames; |
|
73 | $callback($dictionary); |
||
74 | } |
||
75 | } |
||
76 | } |
||
77 |
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.