Skip to content

Commit aa209d0

Browse files
committed
reduce code complexity
1 parent 5837d88 commit aa209d0

File tree

8 files changed

+136
-78
lines changed

8 files changed

+136
-78
lines changed

phpmd.xml

+9-1
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,17 @@
1010
<exclude name="MissingImport" />
1111
<exclude name="StaticAccess" />
1212
</rule>
13-
<rule ref="rulesets/codesize.xml" />
13+
<rule ref="rulesets/codesize.xml" >
14+
<exclude name="CyclomaticComplexity" />
15+
</rule>
1416
<rule ref="rulesets/design.xml" />
1517
<rule ref="rulesets/naming.xml" />
1618
<rule ref="rulesets/unusedcode.xml" />
1719
<rule ref="rulesets/controversial.xml" />
20+
21+
<rule ref="rulesets/codesize.xml/CyclomaticComplexity">
22+
<properties>
23+
<property name="reportLevel" description="The Cyclomatic Complexity reporting threshold" value="9"/>
24+
</properties>
25+
</rule>
1826
</ruleset>

src/Composer/Commands/ValidateCommand.php

+30-3
Original file line numberDiff line numberDiff line change
@@ -277,11 +277,30 @@ private function collectOrphans(array $files, array $patches, array $paths, arra
277277

278278
$groups = array_fill_keys(array_keys($paths), array());
279279

280+
$results = array(
281+
$this->findWithMissingConfig($orphanFiles, $paths),
282+
$this->findWithMissingFile($orphanConfig, $paths, $statuses)
283+
);
284+
285+
foreach ($results as $result) {
286+
foreach ($result as $ownerName => $items) {
287+
$groups[$ownerName] = array_merge($groups[$ownerName], $items);
288+
}
289+
}
290+
291+
return array_filter($groups);
292+
}
293+
294+
private function findWithMissingConfig($orphanFiles, $paths)
295+
{
296+
$pathFlags = array_fill_keys(array_keys($paths), true);
297+
$groups = array();
298+
280299
foreach ($orphanFiles as $path => $config) {
281300
$ownerName = $config[Patch::OWNER];
282301
$installPath = $paths[$ownerName];
283302

284-
if (!isset($groups[$ownerName])) {
303+
if (!isset($pathFlags[$ownerName])) {
285304
continue;
286305
}
287306

@@ -291,11 +310,19 @@ private function collectOrphans(array $files, array $patches, array $paths, arra
291310
);
292311
}
293312

313+
return $groups;
314+
}
315+
316+
private function findWithMissingFile($orphanConfig, $paths, $statuses)
317+
{
318+
$pathFlags = array_fill_keys(array_keys($paths), true);
319+
$groups = array();
320+
294321
foreach ($orphanConfig as $path => $config) {
295322
$ownerName = $config[Patch::OWNER];
296323
$installPath = $paths[$ownerName];
297324

298-
if (!isset($groups[$ownerName])) {
325+
if (!isset($pathFlags[$ownerName])) {
299326
continue;
300327
}
301328

@@ -307,7 +334,7 @@ private function collectOrphans(array $files, array $patches, array $paths, arra
307334
);
308335
}
309336

310-
return array_filter($groups);
337+
return $groups;
311338
}
312339

313340
private function outputOrphans(OutputInterface $output, array $groups)

src/Console/OutputAnalyser.php

+3-6
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,9 @@ class OutputAnalyser
99
{
1010
public function scanOutputForFailures($output, $separatorMatcher, array $failureMessages)
1111
{
12-
$patternsWithResults = array_filter(
13-
$failureMessages,
14-
function ($pattern) use ($output) {
15-
return $pattern && preg_match($pattern, $output);
16-
}
17-
);
12+
$patternsWithResults = array_filter($failureMessages, function ($pattern) use ($output) {
13+
return $pattern && preg_match($pattern, $output);
14+
});
1815

1916
if (!$patternsWithResults) {
2017
return array();

src/Patch/Definition/NormalizerComponents/BasePathComponent.php

+14-5
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,12 @@ public function __construct()
2323

2424
public function normalize($target, $label, array $data, array $ownerConfig)
2525
{
26-
if (!isset($ownerConfig[PluginConfig::PATCHES_BASE])) {
26+
if ($this->shouldSkip($ownerConfig, $data)) {
2727
return array();
2828
}
2929

3030
$source = $data[PatchDefinition::SOURCE];
3131

32-
if (parse_url($source, PHP_URL_SCHEME)) {
33-
return array();
34-
}
35-
3632
if (is_numeric($label) && isset($data[PatchDefinition::LABEL])) {
3733
$label = $data[PatchDefinition::LABEL];
3834
}
@@ -98,6 +94,19 @@ public function normalize($target, $label, array $data, array $ownerConfig)
9894
);
9995
}
10096

97+
private function shouldSkip(array $data, array $ownerConfig)
98+
{
99+
if (!isset($ownerConfig[PluginConfig::PATCHES_BASE])) {
100+
return true;
101+
}
102+
103+
if (parse_url($data[PatchDefinition::SOURCE], PHP_URL_SCHEME)) {
104+
return true;
105+
}
106+
107+
return false;
108+
}
109+
101110
private function normalizeLabelForSourcePath($label, $sourcePath)
102111
{
103112
$filename = basename($sourcePath);

src/Patch/DefinitionList/LoaderComponents/TargetsResolverComponent.php

+37-24
Original file line numberDiff line numberDiff line change
@@ -55,41 +55,54 @@ public function __construct(
5555
public function process(array $patches, array $packagesByName)
5656
{
5757
foreach ($patches as $patchTarget => $packagePatches) {
58-
foreach ($packagePatches as $index => $info) {
59-
$targets = isset($info[PatchDefinition::TARGETS])
60-
? $info[PatchDefinition::TARGETS]
61-
: array();
58+
$targets = $this->collectTargets($packagesByName, $packagePatches);
6259

63-
if (!in_array(PatchDefinition::BUNDLE_TARGET, $targets, true)) {
64-
continue;
65-
}
60+
foreach ($targets as $index => $items) {
61+
$patches[$patchTarget][$index][PatchDefinition::TARGETS] = $items;
62+
}
63+
}
6664

67-
if (count($targets) > 1) {
68-
continue;
69-
}
65+
return $patches;
66+
}
7067

71-
$path = $info[PatchDefinition::PATH];
72-
$source = $info[PatchDefinition::SOURCE];
68+
private function collectTargets($packagesByName, $packagePatches)
69+
{
70+
$result = array();
7371

74-
if (!file_exists($path)) {
75-
throw $this->createError('patch file not found', $source);
76-
}
72+
foreach ($packagePatches as $index => $info) {
73+
$targets = isset($info[PatchDefinition::TARGETS])
74+
? $info[PatchDefinition::TARGETS]
75+
: array();
7776

78-
$paths = $this->patchFileAnalyser->getAllPaths(
79-
$this->patchFileLoader->loadWithNormalizedLineEndings($path)
80-
);
77+
if (!in_array(PatchDefinition::BUNDLE_TARGET, $targets, true)) {
78+
continue;
79+
}
8180

82-
$bundleTargets = $this->packageInfoResolver->resolveNamesFromPaths($packagesByName, $paths);
81+
if (count($targets) > 1) {
82+
continue;
83+
}
8384

84-
if (!$bundleTargets && !$this->gracefulMode) {
85-
throw $this->createError('zero matches', $source);
86-
}
85+
$path = $info[PatchDefinition::PATH];
86+
$source = $info[PatchDefinition::SOURCE];
8787

88-
$patches[$patchTarget][$index][PatchDefinition::TARGETS] = array_unique($bundleTargets);
88+
if (!file_exists($path)) {
89+
throw $this->createError('patch file not found', $source);
8990
}
91+
92+
$paths = $this->patchFileAnalyser->getAllPaths(
93+
$this->patchFileLoader->loadWithNormalizedLineEndings($path)
94+
);
95+
96+
$bundleTargets = $this->packageInfoResolver->resolveNamesFromPaths($packagesByName, $paths);
97+
98+
if (!$bundleTargets && !$this->gracefulMode) {
99+
throw $this->createError('zero matches', $source);
100+
}
101+
102+
$result[$index] = array_unique($bundleTargets);
90103
}
91104

92-
return $patches;
105+
return $result;
93106
}
94107

95108
private function createError($reason, $source)

src/Patch/File/Applier.php

+20-21
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,6 @@ class Applier
3535
*/
3636
private $templateUtils;
3737

38-
/**
39-
* @var \Vaimo\ComposerPatches\Utils\DataUtils
40-
*/
41-
private $dataUtils;
42-
4338
/**
4439
* @var \Vaimo\ComposerPatches\Console\OutputAnalyser
4540
*/
@@ -64,7 +59,6 @@ public function __construct(
6459
$this->shell = new \Vaimo\ComposerPatches\Shell($logger);
6560
$this->applierUtils = new \Vaimo\ComposerPatches\Utils\ConfigUtils();
6661
$this->templateUtils = new \Vaimo\ComposerPatches\Utils\TemplateUtils();
67-
$this->dataUtils = new \Vaimo\ComposerPatches\Utils\DataUtils();
6862
$this->outputAnalyser = new \Vaimo\ComposerPatches\Console\OutputAnalyser();
6963
$this->applierErrorFactory = new \Vaimo\ComposerPatches\Factories\ApplierErrorFactory();
7064
}
@@ -226,21 +220,7 @@ private function resolveOperationOutput($applierOperations, $args, $operationFai
226220
$cwd = $this->extractStringValue($args, PluginConfig::PATCHER_ARG_CWD);
227221
$resultKey = sprintf('%s | %s', $cwd, $command);
228222

229-
if ($passOnFailure) {
230-
$this->logger->writeVerbose(
231-
\Vaimo\ComposerPatches\Logger::TYPE_NONE,
232-
'<comment>***</comment> '
233-
. 'The expected result to execution is a failure'
234-
. '<comment>***</comment>'
235-
);
236-
}
237-
238-
if (isset($this->resultCache[$resultKey])) {
239-
$this->logger->writeVerbose(
240-
\Vaimo\ComposerPatches\Logger::TYPE_NONE,
241-
sprintf('(using cached result for: %s = %s)', $command, reset($this->resultCache[$resultKey]))
242-
);
243-
}
223+
$this->outputBehaviourContextInfo($command, $resultKey, $passOnFailure);
244224

245225
if (!isset($this->resultCache[$resultKey])) {
246226
$this->resultCache[$resultKey] = $this->shell->execute($command, $cwd);
@@ -266,6 +246,25 @@ private function resolveOperationOutput($applierOperations, $args, $operationFai
266246
return array(false, $output);
267247
}
268248

249+
private function outputBehaviourContextInfo($command, $resultKey, $passOnFailure)
250+
{
251+
if ($passOnFailure) {
252+
$this->logger->writeVerbose(
253+
\Vaimo\ComposerPatches\Logger::TYPE_NONE,
254+
'<comment>***</comment> '
255+
. 'The expected result to execution is a failure'
256+
. '<comment>***</comment>'
257+
);
258+
}
259+
260+
if (isset($this->resultCache[$resultKey])) {
261+
$this->logger->writeVerbose(
262+
\Vaimo\ComposerPatches\Logger::TYPE_NONE,
263+
sprintf('(using cached result for: %s = %s)', $command, reset($this->resultCache[$resultKey]))
264+
);
265+
}
266+
}
267+
269268
private function validateOutput($output, $operationFailures)
270269
{
271270
$pathMarker = '\|\+\+\+\s(?P<match>.*?)(\t|$)';

src/Patch/SourceLoaders/PatchesSearch.php

+22-17
Original file line numberDiff line numberDiff line change
@@ -208,23 +208,7 @@ private function resolveBaseInfo(array $data)
208208
{
209209
$target = false;
210210

211-
$package = $this->extractSingleValue($data, PatchDefinition::PACKAGE);
212-
$depends = $this->extractSingleValue($data, PatchDefinition::DEPENDS);
213-
$version = $this->extractSingleValue($data, PatchDefinition::VERSION, '>=0.0.0');
214-
215-
if (strpos($version, ':') !== false) {
216-
$valueParts = explode(':', $version);
217-
218-
$depends = trim(array_shift($valueParts));
219-
$version = trim(implode(':', $valueParts));
220-
}
221-
222-
if (strpos($package, ':') !== false) {
223-
$valueParts = explode(':', $package);
224-
225-
$package = trim(array_shift($valueParts));
226-
$version = trim(implode(':', $valueParts));
227-
}
211+
list($package, $version, $depends) = $this->resolveBaseData($data);
228212

229213
if ($package) {
230214
$target = $package;
@@ -259,6 +243,27 @@ private function resolveBaseInfo(array $data)
259243
);
260244
}
261245

246+
private function resolveBaseData(array $data)
247+
{
248+
$package = $this->extractSingleValue($data, PatchDefinition::PACKAGE);
249+
$depends = $this->extractSingleValue($data, PatchDefinition::DEPENDS);
250+
$version = $this->extractSingleValue($data, PatchDefinition::VERSION, '>=0.0.0');
251+
252+
if (strpos($version, ':') !== false) {
253+
$valueParts = explode(':', $version);
254+
$depends = trim(array_shift($valueParts));
255+
$version = trim(implode(':', $valueParts));
256+
}
257+
258+
if (strpos($package, ':') !== false) {
259+
$valueParts = explode(':', $package);
260+
$package = trim(array_shift($valueParts));
261+
$version = trim(implode(':', $valueParts));
262+
}
263+
264+
return array($package, $version, $depends);
265+
}
266+
262267
private function normalizeDependencies($dependsList)
263268
{
264269
$dependsNormalized = array_map(

src/Utils/ComposerConfigUtils.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ public function resolveConstraintPackages(\Composer\Config $composerConfig)
3232

3333
return $platformPackages;
3434
}
35-
}
35+
}

0 commit comments

Comments
 (0)