Skip to content

Commit

Permalink
Dependency types are not hard-coded literals
Browse files Browse the repository at this point in the history
Previously we hard-coded the list of dependency types wherever
we needed to use them. In order to manage language packs within
Whippet, we now need to add to the list of dependency types.
So, rather than have them scattered over the code, we now have
them in one place.

Once we have dropped PHP 7.4 support, we should really use an
enumeration here.

Fixes #70
  • Loading branch information
snim2 committed Nov 26, 2023
1 parent c2137a7 commit 8703f26
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 7 deletions.
9 changes: 9 additions & 0 deletions spec/dependencies/dependency_types.spec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

describe(\Dxw\Whippet\Dependencies\DependencyTypes::class, function () {
context("getDependencyTypes()", function () {
it('returns a list of dependencies as strings', function () {
expect(\Dxw\Whippet\Dependencies\DependencyTypes::getDependencyTypes())->toBe(['themes', 'plugins']);
});
});
});
16 changes: 16 additions & 0 deletions src/Dependencies/DependencyTypes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Dxw\Whippet\Dependencies;

class DependencyTypes
{
public const PLUGINS = 'plugins';
public const THEMES = 'themes';


public static function getDependencyTypes()
{

return [DependencyTypes::THEMES, DependencyTypes::PLUGINS];
}
}
3 changes: 1 addition & 2 deletions src/Dependencies/Describer.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ public function describe()
if ($resultLoad->isErr()) {
return $resultLoad;
}
$dependencyTypes = ['themes', 'plugins'];
$git = new \Dxw\Whippet\Git\Git($this->dir);
$results = [];
foreach ($dependencyTypes as $type) {
foreach (DependencyTypes::getDependencyTypes() as $type) {
foreach ($this->lockFile->getDependencies($type) as $dep) {
$result = $git::tag_for_commit($dep['src'], $dep['revision']);
if ($result->isErr()) {
Expand Down
2 changes: 1 addition & 1 deletion src/Dependencies/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function installAll()

$dependencies = [];

foreach (['themes', 'plugins'] as $type) {
foreach (DependencyTypes::getDependencyTypes() as $type) {
$dependencies[$type] = $this->lockFile->getDependencies($type);
}

Expand Down
6 changes: 3 additions & 3 deletions src/Dependencies/Updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function updateAll()

$allDependencies = [];

foreach (['themes', 'plugins'] as $type) {
foreach (DependencyTypes::getDependencyTypes() as $type) {
$allDependencies[$type] = $this->jsonFile->getDependencies($type);
}

Expand Down Expand Up @@ -133,7 +133,7 @@ private function updateHash()

private function createGitIgnore()
{
foreach (['themes', 'plugins'] as $type) {
foreach (DependencyTypes::getDependencyTypes() as $type) {
foreach ($this->jsonFile->getDependencies($type) as $dep) {
$this->addDependencyToIgnoresArray($type, $dep['name']);
}
Expand All @@ -151,7 +151,7 @@ private function loadGitignore()
}

// Iterate through locked dependencies and remove from gitignore
foreach (['themes', 'plugins'] as $type) {
foreach (DependencyTypes::getDependencyTypes() as $type) {
foreach ($this->lockFile->getDependencies($type) as $dep) {
$line = $this->getGitignoreDependencyLine($type, $dep['name']);
$index = array_search($line, $this->ignores);
Expand Down
2 changes: 1 addition & 1 deletion src/Dependencies/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function validate()

// Check that entries in whippet.json
// match entries in whippet.lock
foreach (['themes', 'plugins'] as $type) {
foreach (DependencyTypes::getDependencyTypes() as $type) {
$whippetJsonDependencies = $whippetJson->getDependencies($type);
$whippetLockDependencies = $whippetLock->getDependencies($type);
if (count($whippetJsonDependencies) !== count($whippetLockDependencies)) {
Expand Down

0 comments on commit 8703f26

Please sign in to comment.