Skip to content

Commit

Permalink
PSR-2 pass
Browse files Browse the repository at this point in the history
  • Loading branch information
moufmouf committed Jan 6, 2017
1 parent 5220442 commit 17428c8
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 79 deletions.
4 changes: 3 additions & 1 deletion src/DiscoveryInterface.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
declare(strict_types=1);

declare (strict_types = 1);

namespace TheCodingMachine\Discovery;

Expand All @@ -14,6 +15,7 @@ interface DiscoveryInterface
* If no assets are found, an empty array is returned.
*
* @param string $assetType
*
* @return array
*/
public function get(string $assetType) : array;
Expand Down
19 changes: 10 additions & 9 deletions src/DiscoveryPlugin.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
<?php
declare(strict_types=1);

namespace TheCodingMachine\Discovery;
declare (strict_types = 1);

namespace TheCodingMachine\Discovery;

use Composer\Composer;
use Composer\EventDispatcher\EventSubscriberInterface;
use Composer\IO\IOInterface;
use Composer\Package\PackageInterface;
use Composer\Plugin\PluginInterface;
use Composer\Repository\InstalledFilesystemRepository;
use Composer\Script\Event;
use Composer\Script\ScriptEvents;
use Seld\JsonLint\JsonParser;
Expand All @@ -26,9 +25,9 @@ class DiscoveryPlugin implements PluginInterface, EventSubscriberInterface
protected $io;

/**
* Apply plugin modifications to Composer
* Apply plugin modifications to Composer.
*
* @param Composer $composer
* @param Composer $composer
* @param IOInterface $io
*/
public function activate(Composer $composer, IOInterface $io)
Expand All @@ -40,7 +39,7 @@ public function activate(Composer $composer, IOInterface $io)
public static function getSubscribedEvents()
{
return [
ScriptEvents::PRE_AUTOLOAD_DUMP => 'beforeDumpAutoload'
ScriptEvents::PRE_AUTOLOAD_DUMP => 'beforeDumpAutoload',
];
}

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


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

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

return array_filter($orderedPackageList, function(PackageInterface $package) {
return array_filter($orderedPackageList, function (PackageInterface $package) {
$installationManager = $this->composer->getInstallationManager();

$packageInstallPath = $installationManager->getInstallPath($package);
Expand All @@ -92,7 +90,9 @@ private function getDiscoveryPackages()
* Returns the parsed JSON of the discovery.json file of a package.
*
* @param PackageInterface $package
*
* @return array
*
* @throws \TheCodingMachine\Discovery\Utils\JsonException
*/
private function getDiscoveryJson(PackageInterface $package) : array
Expand Down Expand Up @@ -120,6 +120,7 @@ private function getDiscoveryJson(PackageInterface $package) : array
* Builds the array that will be exported in the generated TheCodingMachine\Discovery class.
*
* @param PackageInterface[] $discoveryPackages
*
* @return array
*/
private function buildFinalArray(array $discoveryPackages) : array
Expand All @@ -132,7 +133,7 @@ private function buildFinalArray(array $discoveryPackages) : array
foreach ($json as $key => $values) {
$existingValues = $array[$key] ?? [];
if (!is_array($values)) {
$values = [ $values ];
$values = [$values];
}
$existingValues = array_merge($existingValues, $values);
$array[$key] = $existingValues;
Expand Down
123 changes: 64 additions & 59 deletions src/PackagesOrderer.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
declare(strict_types=1);

declare (strict_types = 1);

namespace TheCodingMachine\Discovery;

Expand All @@ -10,70 +11,74 @@
*
* @author David Negrier
*/
class PackagesOrderer {
class PackagesOrderer
{
/**
* Method: go through the tree, loading child first.
* Each time we go through a package, lets ensure the package is not already part of the packages to install.
* If so, ignore.
*
* @param PackageInterface[] $unorderedPackagesList
*
* @return array|PackageInterface[]
*/
public static function reorderPackages(array $unorderedPackagesList) : array {
// The very first step is to reorder the packages alphabetically.
// This is to ensure the same order every time, even between packages that are unrelated.
usort($unorderedPackagesList, function(PackageInterface $packageA, PackageInterface $packageB) {
return strcmp($packageA->getName(), $packageB->getName());
});
$orderedPackagesList = array();
foreach ($unorderedPackagesList as $package) {
$orderedPackagesList = self::walkPackagesList($package, $orderedPackagesList, $unorderedPackagesList);
}
return $orderedPackagesList;
}
/**
* Function used to sort packages by dependencies (packages depending from no other package in front of others)
* Invariant hypothesis for this function: $orderedPackagesList is already ordered and the package we add
* has all its dependencies already accounted for. If not, we add the dependencies first.
*
* @param PackageInterface $package
* @param PackageInterface[] $orderedPackagesList The list of sorted packages
* @param PackageInterface[] $availablePackages The list of all packages not yet sorted
* @return PackageInterface[]
*/
private static function walkPackagesList(PackageInterface $package, array $orderedPackagesList, array &$availablePackages) : array {
// First, let's check that the package we want to add is not already in our list.
foreach ($orderedPackagesList as $includedPackage) {
if ($includedPackage->equals($package)) {
return $orderedPackagesList;
}
}
// We need to make sure there is no loop (if a package A requires a package B that requires the package A)...
// We do that by removing the package from the list of all available packages.
$key = array_search($package, $availablePackages);
unset($availablePackages[$key]);
// Now, let's see if there are dependencies.
foreach ($package->getRequires() as $require) {
/* @var $require Link */
foreach ($availablePackages as $iterPackage) {
if ($iterPackage->getName() == $require->getTarget()) {
$orderedPackagesList = self::walkPackagesList($iterPackage, $orderedPackagesList, $availablePackages);
break;
}
}
}
// FIXME: manage dev-requires and "provides"
// Finally, let's add the package once all dependencies have been added.
$orderedPackagesList[] = $package;
return $orderedPackagesList;
}
public static function reorderPackages(array $unorderedPackagesList) : array
{
// The very first step is to reorder the packages alphabetically.
// This is to ensure the same order every time, even between packages that are unrelated.
usort($unorderedPackagesList, function (PackageInterface $packageA, PackageInterface $packageB) {
return strcmp($packageA->getName(), $packageB->getName());
});

$orderedPackagesList = array();
foreach ($unorderedPackagesList as $package) {
$orderedPackagesList = self::walkPackagesList($package, $orderedPackagesList, $unorderedPackagesList);
}

return $orderedPackagesList;
}

/**
* Function used to sort packages by dependencies (packages depending from no other package in front of others)
* Invariant hypothesis for this function: $orderedPackagesList is already ordered and the package we add
* has all its dependencies already accounted for. If not, we add the dependencies first.
*
* @param PackageInterface $package
* @param PackageInterface[] $orderedPackagesList The list of sorted packages
* @param PackageInterface[] $availablePackages The list of all packages not yet sorted
*
* @return PackageInterface[]
*/
private static function walkPackagesList(PackageInterface $package, array $orderedPackagesList, array &$availablePackages) : array
{
// First, let's check that the package we want to add is not already in our list.
foreach ($orderedPackagesList as $includedPackage) {
if ($includedPackage->equals($package)) {
return $orderedPackagesList;
}
}

// We need to make sure there is no loop (if a package A requires a package B that requires the package A)...
// We do that by removing the package from the list of all available packages.
$key = array_search($package, $availablePackages);
unset($availablePackages[$key]);

// Now, let's see if there are dependencies.
foreach ($package->getRequires() as $require) {
/* @var $require Link */
foreach ($availablePackages as $iterPackage) {
if ($iterPackage->getName() == $require->getTarget()) {
$orderedPackagesList = self::walkPackagesList($iterPackage, $orderedPackagesList, $availablePackages);
break;
}
}
}

// FIXME: manage dev-requires and "provides"

// Finally, let's add the package once all dependencies have been added.
$orderedPackagesList[] = $package;

}
return $orderedPackagesList;
}
}
10 changes: 7 additions & 3 deletions src/Utils/FileSystem.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
declare(strict_types=1);

declare (strict_types = 1);

namespace TheCodingMachine\Discovery\Utils;

Expand All @@ -14,7 +15,7 @@ class FileSystem
/**
* Creates a directory recursively.
*
* @param string $dir The directory path
* @param string $dir The directory path
* @param int $mode The directory mode
*
* @throws IOException On any directory creation failure
Expand Down Expand Up @@ -127,6 +128,7 @@ public function tempnam(string $dir, string $prefix) : string
if (null !== $scheme && 'gs' !== $scheme) {
return $scheme.'://'.$tmpFile;
}

return $tmpFile;
}
throw new IOException('A temporary file could not be created.');
Expand All @@ -144,6 +146,7 @@ public function tempnam(string $dir, string $prefix) : string
}
// Close the file if it was successfully opened
@fclose($handle);

return $tmpFile;
}
throw new IOException('A temporary file could not be created.');
Expand All @@ -159,6 +162,7 @@ public function tempnam(string $dir, string $prefix) : string
private function getSchemeAndHierarchy(string $filename) : array
{
$components = explode('://', $filename, 2);

return 2 === count($components) ? array($components[0], $components[1]) : array(null, $components[0]);
}
}
}
6 changes: 3 additions & 3 deletions src/Utils/IOException.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php
declare(strict_types=1);

namespace TheCodingMachine\Discovery\Utils;
declare (strict_types = 1);

namespace TheCodingMachine\Discovery\Utils;

class IOException extends \RuntimeException
{
Expand All @@ -22,4 +22,4 @@ public function getPath()
{
return $this->path;
}
}
}
7 changes: 3 additions & 4 deletions src/Utils/JsonException.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<?php
declare(strict_types=1);

namespace TheCodingMachine\Discovery\Utils;
declare (strict_types = 1);

namespace TheCodingMachine\Discovery\Utils;

class JsonException extends \RuntimeException
{

}
}

0 comments on commit 17428c8

Please sign in to comment.