Skip to content

Commit

Permalink
Refactor code and enhance readability
Browse files Browse the repository at this point in the history
This commit consists of multiple changes such as refactoring the code syntax for uniformity, removing unnecessary white spaces, commenting and method documentation, and improving readability by reordering import statements alphabetically. Additionally, exception messages were encapsulated in curly braces to allow for variable interpretation. This refactoring improves readability making it easier for other developers to understand and contribute to the project.
  • Loading branch information
deligoez committed Aug 22, 2023
1 parent 684bd75 commit 01339b8
Show file tree
Hide file tree
Showing 7 changed files with 370 additions and 71 deletions.
67 changes: 25 additions & 42 deletions src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace Phonyland\Framework;

use RuntimeException;
use Flow\JSONPath\JSONPath;
use Phonyland\Framework\Exceptions\ShouldNotHappen;
use RuntimeException;

abstract class Generator
{
Expand Down Expand Up @@ -57,9 +57,7 @@ public function __construct(
/**
* Calls a magic attribute.
*
* @param string $name
*
* @return mixed
*
* @throws \Flow\JSONPath\JSONPathException
*/
Expand All @@ -80,26 +78,20 @@ public function __get(string $name): mixed
return call_user_func_array([$this, $name], $this->methodsAsAttributes[$name]);
}

throw new RuntimeException("The $name attribute is not defined!");
throw new RuntimeException("The {$name} attribute is not defined!");
}

/**
* Setting a magic attribute is not allowed.
*
* @param string $name
* @param mixed $value
*
* @return void
*/
public function __set(string $name, mixed $value): void
{
throw new RuntimeException("Setting $name attribute is not allowed!");
throw new RuntimeException("Setting {$name} attribute is not allowed!");
}

/**
* Checks if a magic attribute exists.
*
* @param string $name
*
* @return bool
*/
Expand All @@ -126,18 +118,15 @@ public function __isset(string $name)
/**
* Calls a magic method.
*
* @param string $name
* @param array<mixed> $arguments
*
* @return mixed
* @param array<mixed> $arguments
*/
public function __call(string $name, array $arguments): mixed
{
if (isset($this->methodAliases[$name])) {
return call_user_func_array([$this, $this->methodAliases[$name]], $arguments);
}

throw new RuntimeException("The $name method is not defined!");
throw new RuntimeException("The {$name} method is not defined!");
}

// endregion
Expand All @@ -148,8 +137,6 @@ public function __call(string $name, array $arguments): mixed
* Builds the file path for the generator data file.
*
* @param array<string> $dataPathParts
*
* @return string
*/
protected function buildDataPath(array $dataPathParts): string
{
Expand All @@ -160,53 +147,51 @@ protected function buildDataPath(array $dataPathParts): string

// If we are the generator package alone
if (str_contains(getcwd(), $this->name)) {
return getcwd() .
'/data/' .
implode('/', $dataPathParts) .
return getcwd().
'/data/'.
implode('/', $dataPathParts).
'.php';
}

// If we are using the generator package through another package
return getcwd() .
'/vendor/' .
$this->name .
'/data/' .
implode('/', $dataPathParts) .
return getcwd().
'/vendor/'.
$this->name.
'/data/'.
implode('/', $dataPathParts).
'.php';
}

return getcwd() .
'/vendor/' .
$this->dataPackages[$this->phony->defaultLocale] .
'/data/' .
implode('/', $dataPathParts) .
return getcwd().
'/vendor/'.
$this->dataPackages[$this->phony->defaultLocale].
'/data/'.
implode('/', $dataPathParts).
'.php';
}

/**
* Fetches data by given path.
*
* @param string $path
*
* @return mixed
*
* @throws \Flow\JSONPath\JSONPathException
*/
protected function fetch(string $path): mixed
{
[$dataPath, $inlinePath] = explode('::', $path) + [1 => null];
$dataPathParts = explode('.', $dataPath);
$alias = $dataPathParts[0];
$dataPathParts = explode('.', $dataPath);
$alias = $dataPathParts[0];
unset($dataPathParts[0]);

if (! $this->hasDataPackageForDefaultLocale()) {
throw ShouldNotHappen::fromMessage("The generator $this->alias does not have any data file for the {$this->phony->defaultLocale} locale.");
if (!$this->hasDataPackageForDefaultLocale()) {
throw ShouldNotHappen::fromMessage("The generator {$this->alias} does not have any data file for the {$this->phony->defaultLocale} locale.");
}

$filePath = $this->buildDataPath(array_values($dataPathParts));

if (! file_exists($filePath)) {
throw ShouldNotHappen::fromMessage("Data file does not exist at path $filePath");
if (!file_exists($filePath)) {
throw ShouldNotHappen::fromMessage("Data file does not exist at path {$filePath}");
}

$data = require $filePath;
Expand Down Expand Up @@ -257,10 +242,8 @@ protected function fetch(string $path): mixed
* Set one or many data packages for the generator.
*
* @param array<string, string>|null $dataPackages
*
* @return void
*/
public function setDataPackages(?array $dataPackages = null): void
public function setDataPackages(array $dataPackages = null): void
{
if ($dataPackages === [] || $dataPackages === null) {
return;
Expand Down
Loading

0 comments on commit 01339b8

Please sign in to comment.