Skip to content

Commit 17428c8

Browse files
committed
PSR-2 pass
1 parent 5220442 commit 17428c8

File tree

6 files changed

+90
-79
lines changed

6 files changed

+90
-79
lines changed

src/DiscoveryInterface.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
2-
declare(strict_types=1);
2+
3+
declare (strict_types = 1);
34

45
namespace TheCodingMachine\Discovery;
56

@@ -14,6 +15,7 @@ interface DiscoveryInterface
1415
* If no assets are found, an empty array is returned.
1516
*
1617
* @param string $assetType
18+
*
1719
* @return array
1820
*/
1921
public function get(string $assetType) : array;

src/DiscoveryPlugin.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
<?php
2-
declare(strict_types=1);
32

4-
namespace TheCodingMachine\Discovery;
3+
declare (strict_types = 1);
54

5+
namespace TheCodingMachine\Discovery;
66

77
use Composer\Composer;
88
use Composer\EventDispatcher\EventSubscriberInterface;
99
use Composer\IO\IOInterface;
1010
use Composer\Package\PackageInterface;
1111
use Composer\Plugin\PluginInterface;
12-
use Composer\Repository\InstalledFilesystemRepository;
1312
use Composer\Script\Event;
1413
use Composer\Script\ScriptEvents;
1514
use Seld\JsonLint\JsonParser;
@@ -26,9 +25,9 @@ class DiscoveryPlugin implements PluginInterface, EventSubscriberInterface
2625
protected $io;
2726

2827
/**
29-
* Apply plugin modifications to Composer
28+
* Apply plugin modifications to Composer.
3029
*
31-
* @param Composer $composer
30+
* @param Composer $composer
3231
* @param IOInterface $io
3332
*/
3433
public function activate(Composer $composer, IOInterface $io)
@@ -40,7 +39,7 @@ public function activate(Composer $composer, IOInterface $io)
4039
public static function getSubscribedEvents()
4140
{
4241
return [
43-
ScriptEvents::PRE_AUTOLOAD_DUMP => 'beforeDumpAutoload'
42+
ScriptEvents::PRE_AUTOLOAD_DUMP => 'beforeDumpAutoload',
4443
];
4544
}
4645

@@ -54,7 +53,6 @@ public function beforeDumpAutoload(Event $event)
5453
$discoveryPackages = $this->getDiscoveryPackages();
5554
$finalArray = $this->buildFinalArray($discoveryPackages);
5655

57-
5856
$fileSystem = new FileSystem();
5957
$fileSystem->dumpFile('.discovery/discovery_data.php', '<?php
6058
return '.var_export($finalArray, true).";\n");
@@ -79,7 +77,7 @@ private function getDiscoveryPackages()
7977

8078
$orderedPackageList = PackagesOrderer::reorderPackages($unorderedPackagesList);
8179

82-
return array_filter($orderedPackageList, function(PackageInterface $package) {
80+
return array_filter($orderedPackageList, function (PackageInterface $package) {
8381
$installationManager = $this->composer->getInstallationManager();
8482

8583
$packageInstallPath = $installationManager->getInstallPath($package);
@@ -92,7 +90,9 @@ private function getDiscoveryPackages()
9290
* Returns the parsed JSON of the discovery.json file of a package.
9391
*
9492
* @param PackageInterface $package
93+
*
9594
* @return array
95+
*
9696
* @throws \TheCodingMachine\Discovery\Utils\JsonException
9797
*/
9898
private function getDiscoveryJson(PackageInterface $package) : array
@@ -120,6 +120,7 @@ private function getDiscoveryJson(PackageInterface $package) : array
120120
* Builds the array that will be exported in the generated TheCodingMachine\Discovery class.
121121
*
122122
* @param PackageInterface[] $discoveryPackages
123+
*
123124
* @return array
124125
*/
125126
private function buildFinalArray(array $discoveryPackages) : array
@@ -132,7 +133,7 @@ private function buildFinalArray(array $discoveryPackages) : array
132133
foreach ($json as $key => $values) {
133134
$existingValues = $array[$key] ?? [];
134135
if (!is_array($values)) {
135-
$values = [ $values ];
136+
$values = [$values];
136137
}
137138
$existingValues = array_merge($existingValues, $values);
138139
$array[$key] = $existingValues;

src/PackagesOrderer.php

Lines changed: 64 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
2-
declare(strict_types=1);
2+
3+
declare (strict_types = 1);
34

45
namespace TheCodingMachine\Discovery;
56

@@ -10,70 +11,74 @@
1011
*
1112
* @author David Negrier
1213
*/
13-
class PackagesOrderer {
14+
class PackagesOrderer
15+
{
1416
/**
1517
* Method: go through the tree, loading child first.
1618
* Each time we go through a package, lets ensure the package is not already part of the packages to install.
1719
* If so, ignore.
1820
*
1921
* @param PackageInterface[] $unorderedPackagesList
22+
*
2023
* @return array|PackageInterface[]
2124
*/
22-
public static function reorderPackages(array $unorderedPackagesList) : array {
23-
// The very first step is to reorder the packages alphabetically.
24-
// This is to ensure the same order every time, even between packages that are unrelated.
25-
usort($unorderedPackagesList, function(PackageInterface $packageA, PackageInterface $packageB) {
26-
return strcmp($packageA->getName(), $packageB->getName());
27-
});
28-
29-
$orderedPackagesList = array();
30-
foreach ($unorderedPackagesList as $package) {
31-
$orderedPackagesList = self::walkPackagesList($package, $orderedPackagesList, $unorderedPackagesList);
32-
}
33-
34-
return $orderedPackagesList;
35-
}
36-
37-
/**
38-
* Function used to sort packages by dependencies (packages depending from no other package in front of others)
39-
* Invariant hypothesis for this function: $orderedPackagesList is already ordered and the package we add
40-
* has all its dependencies already accounted for. If not, we add the dependencies first.
41-
*
42-
* @param PackageInterface $package
43-
* @param PackageInterface[] $orderedPackagesList The list of sorted packages
44-
* @param PackageInterface[] $availablePackages The list of all packages not yet sorted
45-
* @return PackageInterface[]
46-
*/
47-
private static function walkPackagesList(PackageInterface $package, array $orderedPackagesList, array &$availablePackages) : array {
48-
// First, let's check that the package we want to add is not already in our list.
49-
foreach ($orderedPackagesList as $includedPackage) {
50-
if ($includedPackage->equals($package)) {
51-
return $orderedPackagesList;
52-
}
53-
}
54-
55-
// We need to make sure there is no loop (if a package A requires a package B that requires the package A)...
56-
// We do that by removing the package from the list of all available packages.
57-
$key = array_search($package, $availablePackages);
58-
unset($availablePackages[$key]);
59-
60-
// Now, let's see if there are dependencies.
61-
foreach ($package->getRequires() as $require) {
62-
/* @var $require Link */
63-
foreach ($availablePackages as $iterPackage) {
64-
if ($iterPackage->getName() == $require->getTarget()) {
65-
$orderedPackagesList = self::walkPackagesList($iterPackage, $orderedPackagesList, $availablePackages);
66-
break;
67-
}
68-
}
69-
}
70-
71-
// FIXME: manage dev-requires and "provides"
72-
73-
// Finally, let's add the package once all dependencies have been added.
74-
$orderedPackagesList[] = $package;
75-
76-
return $orderedPackagesList;
77-
}
25+
public static function reorderPackages(array $unorderedPackagesList) : array
26+
{
27+
// The very first step is to reorder the packages alphabetically.
28+
// This is to ensure the same order every time, even between packages that are unrelated.
29+
usort($unorderedPackagesList, function (PackageInterface $packageA, PackageInterface $packageB) {
30+
return strcmp($packageA->getName(), $packageB->getName());
31+
});
32+
33+
$orderedPackagesList = array();
34+
foreach ($unorderedPackagesList as $package) {
35+
$orderedPackagesList = self::walkPackagesList($package, $orderedPackagesList, $unorderedPackagesList);
36+
}
37+
38+
return $orderedPackagesList;
39+
}
40+
41+
/**
42+
* Function used to sort packages by dependencies (packages depending from no other package in front of others)
43+
* Invariant hypothesis for this function: $orderedPackagesList is already ordered and the package we add
44+
* has all its dependencies already accounted for. If not, we add the dependencies first.
45+
*
46+
* @param PackageInterface $package
47+
* @param PackageInterface[] $orderedPackagesList The list of sorted packages
48+
* @param PackageInterface[] $availablePackages The list of all packages not yet sorted
49+
*
50+
* @return PackageInterface[]
51+
*/
52+
private static function walkPackagesList(PackageInterface $package, array $orderedPackagesList, array &$availablePackages) : array
53+
{
54+
// First, let's check that the package we want to add is not already in our list.
55+
foreach ($orderedPackagesList as $includedPackage) {
56+
if ($includedPackage->equals($package)) {
57+
return $orderedPackagesList;
58+
}
59+
}
60+
61+
// We need to make sure there is no loop (if a package A requires a package B that requires the package A)...
62+
// We do that by removing the package from the list of all available packages.
63+
$key = array_search($package, $availablePackages);
64+
unset($availablePackages[$key]);
65+
66+
// Now, let's see if there are dependencies.
67+
foreach ($package->getRequires() as $require) {
68+
/* @var $require Link */
69+
foreach ($availablePackages as $iterPackage) {
70+
if ($iterPackage->getName() == $require->getTarget()) {
71+
$orderedPackagesList = self::walkPackagesList($iterPackage, $orderedPackagesList, $availablePackages);
72+
break;
73+
}
74+
}
75+
}
76+
77+
// FIXME: manage dev-requires and "provides"
78+
79+
// Finally, let's add the package once all dependencies have been added.
80+
$orderedPackagesList[] = $package;
7881

79-
}
82+
return $orderedPackagesList;
83+
}
84+
}

src/Utils/FileSystem.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
2-
declare(strict_types=1);
2+
3+
declare (strict_types = 1);
34

45
namespace TheCodingMachine\Discovery\Utils;
56

@@ -14,7 +15,7 @@ class FileSystem
1415
/**
1516
* Creates a directory recursively.
1617
*
17-
* @param string $dir The directory path
18+
* @param string $dir The directory path
1819
* @param int $mode The directory mode
1920
*
2021
* @throws IOException On any directory creation failure
@@ -127,6 +128,7 @@ public function tempnam(string $dir, string $prefix) : string
127128
if (null !== $scheme && 'gs' !== $scheme) {
128129
return $scheme.'://'.$tmpFile;
129130
}
131+
130132
return $tmpFile;
131133
}
132134
throw new IOException('A temporary file could not be created.');
@@ -144,6 +146,7 @@ public function tempnam(string $dir, string $prefix) : string
144146
}
145147
// Close the file if it was successfully opened
146148
@fclose($handle);
149+
147150
return $tmpFile;
148151
}
149152
throw new IOException('A temporary file could not be created.');
@@ -159,6 +162,7 @@ public function tempnam(string $dir, string $prefix) : string
159162
private function getSchemeAndHierarchy(string $filename) : array
160163
{
161164
$components = explode('://', $filename, 2);
165+
162166
return 2 === count($components) ? array($components[0], $components[1]) : array(null, $components[0]);
163167
}
164-
}
168+
}

src/Utils/IOException.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
2-
declare(strict_types=1);
32

4-
namespace TheCodingMachine\Discovery\Utils;
3+
declare (strict_types = 1);
54

5+
namespace TheCodingMachine\Discovery\Utils;
66

77
class IOException extends \RuntimeException
88
{
@@ -22,4 +22,4 @@ public function getPath()
2222
{
2323
return $this->path;
2424
}
25-
}
25+
}

src/Utils/JsonException.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
<?php
2-
declare(strict_types=1);
32

4-
namespace TheCodingMachine\Discovery\Utils;
3+
declare (strict_types = 1);
54

5+
namespace TheCodingMachine\Discovery\Utils;
66

77
class JsonException extends \RuntimeException
88
{
9-
10-
}
9+
}

0 commit comments

Comments
 (0)