Conditions | 24 |
Paths | 4850 |
Total Lines | 130 |
Lines | 16 |
Ratio | 12.31 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
19 | public function buildOperationList(Index $mergedIndex, Index $localIndex): OperationList |
||
20 | { |
||
21 | $operationList = new OperationList(); |
||
22 | |||
23 | // mtimes to be set for directories are collected and applied afterwards as they get modified by synchronization operations as well |
||
24 | /** @var IndexObject[] $toSetMtime */ |
||
25 | $toSetMtime = []; |
||
26 | |||
27 | // set of modified paths that can be populated and is later used to add parent directory touch()es |
||
28 | $modifiedPaths = []; |
||
29 | |||
30 | // paths to be removed |
||
31 | /** @var IndexObject[] $toUnlink */ |
||
32 | $toUnlink = []; |
||
33 | |||
34 | // relies on the directory tree structure being traversed in pre-order (or at least a directory appears before its content) |
||
35 | foreach ($mergedIndex as $mergedIndexObject) |
||
36 | { |
||
37 | /** @var IndexObject $mergedIndexObject */ |
||
38 | |||
39 | $localObject = $localIndex->getObjectByPath($mergedIndexObject->getRelativePath()); |
||
40 | |||
41 | // unlink to-be-overridden local path with different type |
||
42 | if ($localObject !== null && $localObject->getType() !== $mergedIndexObject->getType()) |
||
43 | { |
||
44 | $toUnlink[] = $mergedIndexObject; |
||
45 | $modifiedPaths[] = $mergedIndexObject->getRelativePath(); |
||
46 | } |
||
47 | |||
48 | |||
49 | if ($mergedIndexObject->isDirectory()) |
||
50 | { |
||
51 | if ($localObject === null || !$localObject->isDirectory()) |
||
52 | { |
||
53 | $operationList->add(new OperationListItem(new MkdirOperation($mergedIndexObject->getRelativePath(), $mergedIndexObject->getPermissions()), $mergedIndexObject)); |
||
54 | |||
55 | $toSetMtime[$mergedIndexObject->getRelativePath()] = $mergedIndexObject; |
||
56 | } |
||
57 | } |
||
58 | |||
59 | elseif ($mergedIndexObject->isFile()) |
||
60 | { |
||
61 | // local file did not exist, hasn't been a file before or has outdated content |
||
62 | $doDownloadFile = $localObject === null || !$localObject->isFile() || $mergedIndexObject->getBlobId() !== $localObject->getBlobId(); |
||
63 | |||
64 | View Code Duplication | if ($doDownloadFile) |
|
|
|||
65 | { |
||
66 | $operationList->add(new OperationListItem(new DownloadOperation($mergedIndexObject->getRelativePath(), $mergedIndexObject->getBlobId()), $mergedIndexObject)); |
||
67 | $operationList->add(new OperationListItem(new TouchOperation($mergedIndexObject->getRelativePath(), $mergedIndexObject->getMtime()), $mergedIndexObject)); |
||
68 | $operationList->add(new OperationListItem(new ChmodOperation($mergedIndexObject->getRelativePath(), $mergedIndexObject->getPermissions()), $mergedIndexObject)); |
||
69 | |||
70 | $modifiedPaths[] = $mergedIndexObject->getRelativePath(); |
||
71 | } |
||
72 | } |
||
73 | |||
74 | elseif ($mergedIndexObject->isLink()) |
||
75 | { |
||
76 | View Code Duplication | if ($localObject !== null && $localObject->getLinkTarget() !== $mergedIndexObject->getLinkTarget()) |
|
77 | { |
||
78 | $operationList->add(new OperationListItem(new UnlinkOperation($mergedIndexObject->getRelativePath()), $mergedIndexObject)); |
||
79 | $operationList->add(new OperationListItem(new SymlinkOperation($mergedIndexObject->getRelativePath(), $mergedIndexObject->getLinkTarget()), $mergedIndexObject)); |
||
80 | $operationList->add(new OperationListItem(new TouchOperation($mergedIndexObject->getRelativePath(), $mergedIndexObject->getPermissions()), $mergedIndexObject)); |
||
81 | |||
82 | $modifiedPaths[] = $mergedIndexObject->getRelativePath(); |
||
83 | } |
||
84 | } |
||
85 | |||
86 | else |
||
87 | { |
||
88 | // unknown/invalid object type |
||
89 | throw new Exception(); |
||
90 | } |
||
91 | |||
92 | |||
93 | if ($localObject !== null) |
||
94 | { |
||
95 | if ($localObject->getPermissions() !== $mergedIndexObject->getPermissions()) |
||
96 | { |
||
97 | $operationList->add(new OperationListItem(new ChmodOperation($mergedIndexObject->getRelativePath(), $mergedIndexObject->getPermissions()), $mergedIndexObject)); |
||
98 | } |
||
99 | |||
100 | if ($localObject->getMtime() !== $mergedIndexObject->getMtime()) |
||
101 | { |
||
102 | $toSetMtime[$localObject->getRelativePath()] = $mergedIndexObject; |
||
103 | } |
||
104 | } |
||
105 | } |
||
106 | |||
107 | // remove superfluous local files |
||
108 | foreach ($localIndex as $localObject) |
||
109 | { |
||
110 | /** @var IndexObject $localObject */ |
||
111 | |||
112 | if ($mergedIndex->getObjectByPath($localObject->getRelativePath()) === null) |
||
113 | { |
||
114 | $toUnlink[] = $localObject; |
||
115 | $modifiedPaths[] = $localObject->getRelativePath(); |
||
116 | } |
||
117 | } |
||
118 | |||
119 | // add modified paths to directory mtimes to be set |
||
120 | foreach ($modifiedPaths as $modifiedPath) |
||
121 | { |
||
122 | if (($dir = dirname($modifiedPath)) !== '.') |
||
123 | { |
||
124 | if ($dirObject = $mergedIndex->getObjectByPath($dir)) |
||
125 | { |
||
126 | $toSetMtime[$dirObject->getRelativePath()] = $dirObject; |
||
127 | } |
||
128 | } |
||
129 | } |
||
130 | |||
131 | // prepend deletions |
||
132 | krsort($toUnlink); |
||
133 | $unlinkOperations = new OperationList(); |
||
134 | foreach ($toUnlink as $indexObject) |
||
135 | { |
||
136 | $unlinkOperations->add(new OperationListItem(new UnlinkOperation($indexObject->getRelativePath()), $indexObject)); |
||
137 | } |
||
138 | $operationList->prepend($unlinkOperations); |
||
139 | |||
140 | // set directory mtimes after all other modifications have been performed |
||
141 | krsort($toSetMtime); |
||
142 | foreach ($toSetMtime as $relativePath => $indexObject) |
||
143 | { |
||
144 | $operationList->add(new OperationListItem(new TouchOperation($relativePath, $indexObject->getMtime()), $indexObject)); |
||
145 | } |
||
146 | |||
147 | return $operationList; |
||
148 | } |
||
149 | } |
||
150 |
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.