Skip to content

Commit

Permalink
Code simplyfi + better code style + better performance + mark busines…
Browse files Browse the repository at this point in the history
…s logic as final.
  • Loading branch information
janbarasek committed Jan 12, 2020
1 parent 1526454 commit d438905
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 144 deletions.
2 changes: 1 addition & 1 deletion src/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Nette\DI\Container;
use Tracy\Debugger;

class Console
final class Console
{

/**
Expand Down
12 changes: 12 additions & 0 deletions src/Exceptions/PackageDescriptorException.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,16 @@ public static function tempFileGeneratingError(string $path): void
);
}

/**
* @param string $path
* @throws PackageDescriptorException
*/
public static function canNotRewritePackageNeon(string $path): void
{
throw new self(
'Can not rewrite package.neon. Path: "' . $path . '"'
. "\n" . error_get_last()['message']
);
}

}
57 changes: 18 additions & 39 deletions src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
use Baraja\PackageManager\Exception\PackageDescriptorException;
use Composer\Autoload\ClassLoader;
use Nette\Neon\Neon;
use Nette\Utils\Strings;

class Generator
final class Generator
{

/**
Expand Down Expand Up @@ -46,19 +45,16 @@ public function __construct(string $projectRoot, array $customPackagesNamePatter
* @return PackageDescriptorEntity
* @throws PackageDescriptorException
*/
public function generate(): PackageDescriptorEntity
public function run(): PackageDescriptorEntity
{
$packageDescriptor = new PackageDescriptorEntity($this->customPackagesNamePatterns);

$composerJson = $this->storage->haystackToArray(
json_decode(
(string) file_get_contents($this->projectRoot . '/composer.json')
)
json_decode((string) file_get_contents($this->projectRoot . '/composer.json'))
);

$packages = $this->getPackages($composerJson);
$packageDescriptor->setComposer($composerJson);
$packageDescriptor->setPackages($packages);
$packageDescriptor->setPackages($packages = $this->getPackages($composerJson));

$customRouters = [];
$afterInstallScripts = [];
Expand All @@ -85,7 +81,7 @@ public function generate(): PackageDescriptorEntity

/**
* @param string[][] $composer
* @return string[][]
* @return string[][]|string[][][]|string[][][][]
* @throws PackageDescriptorException
*/
private function getPackages(array $composer): array
Expand All @@ -98,16 +94,8 @@ private function getPackages(array $composer): array
$packagesVersions = [];
}

$allPackages = array_merge(
$composer['require'],
$packagesVersions
);

foreach ($allPackages as $packageName => $dependency) {
$packageName = Strings::lower($packageName);
$path = $this->projectRoot . '/vendor/' . $packageName;

if (!is_dir($path)) {
foreach (array_merge($composer['require'], $packagesVersions) as $name => $dependency) {
if (is_dir($path = $this->projectRoot . '/vendor/' . ($name = mb_strtolower($name, 'UTF-8'))) === false) {
continue;
}

Expand All @@ -118,26 +106,19 @@ private function getPackages(array $composer): array
$configPath = $path . '/config.neon';
}

$composerPath = $path . '/composer.json';

if (is_file($composerPath) && json_decode(file_get_contents($composerPath)) === null) {
PackageDescriptorCompileException::composerJsonIsBroken($packageName);
if (is_file($composerPath = $path . '/composer.json') && json_decode(file_get_contents($composerPath)) === null) {
PackageDescriptorCompileException::composerJsonIsBroken($name);
}

$item = [
'name' => $packageName,
'version' => $packagesVersions[$packageName] ?? null,
$packages[] = [
'name' => $name,
'version' => $packagesVersions[$name] ?? null,
'dependency' => $dependency,
'config' => $configPath !== null ? $this->formatConfigSections($configPath) : null,
'composer' => is_file($composerPath)
? $this->storage->haystackToArray(
json_decode(
file_get_contents($composerPath)
)
) : null,
? $this->storage->haystackToArray(json_decode(file_get_contents($composerPath)))
: null,
];

$packages[] = $item;
}

return $packages;
Expand All @@ -150,15 +131,13 @@ private function getPackages(array $composer): array
private function formatConfigSections(string $path): array
{
$return = [];
$neon = Neon::decode(file_get_contents($path));

foreach (\is_array($neon) ? $neon : [] as $part => $haystack) {
foreach (\is_array($neon = Neon::decode(file_get_contents($path))) ? $neon : [] as $part => $haystack) {
if ($part === 'services') {
$servicesList = '';

foreach ($haystack as $serviceKey => $serviceClass) {
$servicesList .= (\is_int($serviceKey) ? '- ' : $serviceKey . ': ')
. Neon::encode($serviceClass) . "\n";
foreach ($haystack as $key => $serviceClass) {
$servicesList .= (\is_int($key) ? '- ' : $key . ': ') . Neon::encode($serviceClass) . "\n";
}

$return[$part] = [
Expand Down Expand Up @@ -192,7 +171,7 @@ private function getPackagesVersions(): array
$lockFile = null;
}

if (!is_file($lockFile)) {
if (is_file($lockFile) === false) {
PackageDescriptorCompileException::canNotLoadComposerLock($lockFile);
}

Expand Down
36 changes: 17 additions & 19 deletions src/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,24 @@


use Baraja\PackageManager\Exception\PackageDescriptorException;
use Nette\StaticClass;

class Helpers
final class Helpers
{

use StaticClass;
/**
* @throws \Error
*/
public function __construct()
{
throw new \Error('Class ' . get_class($this) . ' is static and cannot be instantiated.');
}

/**
* @param mixed[] $array1
* @param mixed[] $array2
* @return mixed
* @return mixed[]
*/
public static function recursiveMerge(array &$array1, array &$array2)
public static function recursiveMerge(array &$array1, array &$array2): array
{
$merged = $array1;

Expand All @@ -44,12 +49,8 @@ public static function functionIsAvailable(string $functionName): bool
static $disabled;

if (\function_exists($functionName)) {
if ($disabled === null) {
$disableFunctions = ini_get('disable_functions');

if (\is_string($disableFunctions)) {
$disabled = explode(',', $disableFunctions) ? : [];
}
if ($disabled === null && \is_string($disableFunctions = ini_get('disable_functions'))) {
$disabled = explode(',', $disableFunctions) ? : [];
}

return \in_array($functionName, $disabled, true) === false;
Expand All @@ -67,14 +68,12 @@ public static function functionIsAvailable(string $functionName): bool
public static function terminalRenderCode(string $path, int $line = null): void
{
echo "\n" . $path . ($line === null ? '' : ' [on line ' . $line . ']') . "\n\n";
if (\is_file($path)) {
if (\is_file($path) === true) {
echo '----- file -----' . "\n";
$file = str_replace(["\r\n", "\r"], "\n", (string) file_get_contents($path));
$fileParser = explode("\n", $file);
$start = $line > 8 ? $line - 8 : 0;
$fileParser = explode("\n", str_replace(["\r\n", "\r"], "\n", (string) file_get_contents($path)));

for ($i = $start; $i <= $start + 15; $i++) {
if (!isset($fileParser[$i])) {
for ($i = ($start = $line > 8 ? $line - 8 : 0); $i <= $start + 15; $i++) {
if (isset($fileParser[$i]) === false) {
break;
}

Expand Down Expand Up @@ -135,10 +134,9 @@ public static function terminalInteractiveAsk(string $question, ?array $possibil
throw new PackageDescriptorException('Problem with opening "php://stdin".');
}

$input = trim((string) fgets($fOpen));
echo "\n";

$input = $input === '' ? null : $input;
$input = ($input = trim((string) fgets($fOpen))) === '' ? null : $input;

if ($possibilities !== [] && $possibilities !== null) {
if (\in_array($input, $possibilities, true)) {
Expand Down
2 changes: 1 addition & 1 deletion src/InteractiveComposer/InteractiveComposer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Baraja\PackageManager\Composer\ITask;
use Baraja\PackageManager\Exception\TaskException;

class InteractiveComposer
final class InteractiveComposer
{

/**
Expand Down
11 changes: 4 additions & 7 deletions src/PackageManagerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,10 @@ class PackageManagerExtension extends CompilerExtension
*/
public function afterCompile(ClassType $class): void
{
$initialize = $class->getMethod('initialize');

if (PHP_SAPI === 'cli' && class_exists(Application::class)) {
$initialize->setBody(
$initialize->getBody() . "\n"
. Console::class . '::setContainer($this);' . "\n"
. 'register_shutdown_function([' . Console::class . '::class, \'run\']);'
if (PHP_SAPI === 'cli' && class_exists(Application::class) === true) {
$class->getMethod('initialize')->addBody(
Console::class . '::setContainer($this);'
. "\n" . 'register_shutdown_function([' . Console::class . '::class, \'run\']);'
);
}
}
Expand Down

0 comments on commit d438905

Please sign in to comment.