From 01339b8ec69eb878f5f1a9516eacb057ecf65f44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yunus=20Emre=20Delig=C3=B6z?= Date: Wed, 23 Aug 2023 01:43:43 +0300 Subject: [PATCH] Refactor code and enhance readability 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. --- src/Generator.php | 67 +++--- src/Locale.php | 325 +++++++++++++++++++++++++++++ src/Phony.php | 2 +- tests/Generator/MagicTest.php | 8 +- tests/Pest.php | 34 ++- tests/PipeTest.php | 2 +- tests/Stubs/SampleOneGenerator.php | 3 - 7 files changed, 370 insertions(+), 71 deletions(-) diff --git a/src/Generator.php b/src/Generator.php index a747270..eb8c6ca 100644 --- a/src/Generator.php +++ b/src/Generator.php @@ -4,9 +4,9 @@ namespace Phonyland\Framework; +use RuntimeException; use Flow\JSONPath\JSONPath; use Phonyland\Framework\Exceptions\ShouldNotHappen; -use RuntimeException; abstract class Generator { @@ -57,9 +57,7 @@ public function __construct( /** * Calls a magic attribute. * - * @param string $name * - * @return mixed * * @throws \Flow\JSONPath\JSONPathException */ @@ -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 */ @@ -126,10 +118,7 @@ public function __isset(string $name) /** * Calls a magic method. * - * @param string $name - * @param array $arguments - * - * @return mixed + * @param array $arguments */ public function __call(string $name, array $arguments): mixed { @@ -137,7 +126,7 @@ public function __call(string $name, array $arguments): mixed 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 @@ -148,8 +137,6 @@ public function __call(string $name, array $arguments): mixed * Builds the file path for the generator data file. * * @param array $dataPathParts - * - * @return string */ protected function buildDataPath(array $dataPathParts): string { @@ -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; @@ -257,10 +242,8 @@ protected function fetch(string $path): mixed * Set one or many data packages for the generator. * * @param array|null $dataPackages - * - * @return void */ - public function setDataPackages(?array $dataPackages = null): void + public function setDataPackages(array $dataPackages = null): void { if ($dataPackages === [] || $dataPackages === null) { return; diff --git a/src/Locale.php b/src/Locale.php index 28b0f90..4af3c51 100644 --- a/src/Locale.php +++ b/src/Locale.php @@ -7,329 +7,654 @@ class Locale { public const LocaleIndependent = '*'; + public const English = 'en'; + public const Cebuano = 'ceb'; + public const German = 'de'; + public const Swedish = 'sv'; + public const French = 'fr'; + public const Dutch = 'nl'; + public const Russian = 'ru'; + public const Spanish = 'es'; + public const Italian = 'it'; + public const Egyptian_Arabic = 'arz'; + public const Polish = 'pl'; + public const Japanese = 'ja'; + public const Vietnamese = 'vi'; + public const Waray_Waray = 'war'; + public const Chinese = 'zh'; + public const Arabic = 'ar'; + public const Ukrainian = 'uk'; + public const Portuguese = 'pt'; + public const Persian = 'fa'; + public const Catalan = 'ca'; + public const Serbian = 'sr'; + public const Indonesian = 'id'; + public const Korean = 'ko'; + public const Norwegian_Bokmål = 'no'; + public const Finnish = 'fi'; + public const Hungarian = 'hu'; + public const Czech = 'cs'; + public const Turkish = 'tr'; + public const Chechen = 'ce'; + public const Serbo_Croatian = 'sh'; + public const Min_Nan = 'zh-min-nan'; + public const Romanian = 'ro'; + public const Tatar = 'tt'; + public const Basque = 'eu'; + public const Malay = 'ms'; + public const Esperanto = 'eo'; + public const Hebrew = 'he'; + public const Armenian = 'hy'; + public const Bulgarian = 'bg'; + public const Danish = 'da'; + public const South_Azerbaijani = 'azb'; + public const Slovak = 'sk'; + public const Kazakh = 'kk'; + public const Estonian = 'et'; + public const Minangkabau = 'min'; + public const Belarusian = 'be'; + public const Croatian = 'hr'; + public const Greek = 'el'; + public const Simple_English = 'simple'; + public const Lithuanian = 'lt'; + public const Azerbaijani = 'az'; + public const Galician = 'gl'; + public const Slovenian = 'sl'; + public const Urdu = 'ur'; + public const Norwegian_Nynorsk = 'nn'; + public const Georgian = 'ka'; + public const Hindi = 'hi'; + public const Thai = 'th'; + public const Tamil = 'ta'; + public const Uzbek = 'uz'; + public const Latin = 'la'; + public const Welsh = 'cy'; + public const Asturian = 'ast'; + public const Volapük = 'vo'; + public const Macedonian = 'mk'; + public const Cantonese = 'zh-yue'; + public const Bengali = 'bn'; + public const Latvian = 'lv'; + public const Tajik = 'tg'; + public const Burmese = 'my'; + public const Afrikaans = 'af'; + public const Malagasy = 'mg'; + public const Bosnian = 'bs'; + public const Occitan = 'oc'; + public const Albanian = 'sq'; + public const Low_Saxon = 'nds'; + public const Marathi = 'mr'; + public const Kirghiz = 'ky'; + public const Malayalam = 'ml'; + public const Belarusian_Taraškievica = 'be-tarask'; + public const Telugu = 'te'; + public const Newar_Nepal_Bhasa = 'new'; + public const Breton = 'br'; + public const Swahili = 'sw'; + public const Venetian = 'vec'; + public const Piedmontese = 'pms'; + public const Javanese = 'jv'; + public const Western_Panjabi = 'pnb'; + public const Haitian = 'ht'; + public const Sundanese = 'su'; + public const Luxembourgish = 'lb'; + public const Bashkir = 'ba'; + public const Irish = 'ga'; + public const Silesian = 'szl'; + public const Icelandic = 'is'; + public const Kurdish = 'ku'; + public const Lombard = 'lmo'; + public const Chuvash = 'cv'; + public const West_Frisian = 'fy'; + public const Tagalog = 'tl'; + public const Wu = 'wuu'; + public const Aragonese = 'an'; + public const Scots = 'sco'; + public const Zazaki = 'diq'; + public const Sorani = 'ckb'; + public const Punjabi = 'pa'; + public const Yoruba = 'yo'; + public const Nepali = 'ne'; + public const Bavarian = 'bar'; + public const Ido = 'io'; + public const Gujarati = 'gu'; + public const Alemannic = 'als'; + public const Kannada = 'kn'; + public const Sicilian = 'scn'; + public const Bishnupriya_Manipuri = 'bpy'; + public const Interlingua = 'ia'; + public const Quechua = 'qu'; + public const Mongolian = 'mn'; + public const Kotava = 'avk'; + public const Navajo = 'nv'; + public const Mingrelian = 'xmf'; + public const Sinhalese = 'si'; + public const Crimean_Tatar = 'crh'; + public const Samogitian = 'bat-smg'; + public const Oriya = 'or'; + public const Scottish_Gaelic = 'gd'; + public const Min_Dong = 'cdo'; + public const Ilokano = 'ilo'; + public const North_Frisian = 'frr'; + public const Ossetian = 'os'; + public const Yiddish = 'yi'; + public const Sindhi = 'sd'; + public const Amharic = 'am'; + public const Buginese = 'bug'; + public const Neapolitan = 'nap'; + public const Hausa = 'ha'; + public const Sakha = 'sah'; + public const Upper_Sorbian = 'hsb'; + public const Banyumasan = 'map-bms'; + public const Maithili = 'mai'; + public const Faroese = 'fo'; + public const Limburgish = 'li'; + public const Pashto = 'ps'; + public const Mazandarani = 'mzn'; + public const Emilian_Romagnol = 'eml'; + public const Gorontalo = 'gor'; + public const Acehnese = 'ace'; + public const Balinese = 'ban'; + public const Ladin = 'lld'; + public const Central_Bicolano = 'bcl'; + public const Sanskrit = 'sa'; + public const Walloon = 'wa'; + public const Classical_Chinese = 'zh-classical'; + public const Ligurian = 'lij'; + public const Shan = 'shn'; + public const Zulu = 'zu'; + public const Hill_Mari = 'mrj'; + public const Meadow_Mari = 'mhr'; + public const Fiji_Hindi = 'hif'; + public const Assamese = 'as'; + public const Meitei = 'mni'; + public const Hakka = 'hak'; + public const Western_Armenian = 'hyw'; + public const Tarantino = 'roa-tara'; + public const Kapampangan = 'pam'; + public const Khmer = 'km'; + public const Interlingue = 'ie'; + public const Northern_Sotho = 'nso'; + public const Rusyn = 'rue'; + public const Somali = 'so'; + public const Bihari = 'bh'; + public const Northern_Sami = 'se'; + public const Shona = 'sn'; + public const West_Flemish = 'vls'; + public const Dutch_Low_Saxon = 'nds-nl'; + public const Erzya = 'myv'; + public const Nahuatl = 'nah'; + public const Maori = 'mi'; + public const Santali = 'sat'; + public const Sardinian = 'sc'; + public const Vepsian = 'vep'; + public const Gan = 'gan'; + public const Gilaki = 'glk'; + public const Kabyle = 'kab'; + public const Turkmen = 'tk'; + public const Võro = 'fiu-vro'; + public const Corsican = 'co'; + public const Tibetan = 'bo'; + public const Abkhazian = 'ab'; + public const Komi = 'kv'; + public const Franco_Provençal_Arpitan = 'frp'; + public const Kashubian = 'csb'; + public const Picard = 'pcd'; + public const Cornish = 'kw'; + public const Uyghur = 'ug'; + public const Manx = 'gv'; + public const Udmurt = 'udm'; + public const Aymara = 'ay'; + public const Moroccan_Arabic = 'ary'; + public const Norman = 'nrm'; + public const Zeelandic = 'zea'; + public const Guarani = 'gn'; + public const Banjar = 'bjn'; + public const Maltese = 'mt'; + public const Saraiki = 'skr'; + public const Lezgian = 'lez'; + public const Lingua_Franca_Nova_2 = 'lfn'; + public const Saterland_Frisian = 'stq'; + public const Inari_Sami = 'smn'; + public const Lao = 'lo'; + public const Mirandese = 'mwl'; + public const Livvi_Karelian = 'olo'; + public const Romansh = 'rm'; + public const Friulian = 'fur'; + public const Ladino = 'lad'; + public const Goan_Konkani = 'gom'; + public const Anglo_Saxon = 'ang'; + public const Komi_Permyak = 'koi'; + public const Tuvan = 'tyv'; + public const Extremaduran = 'ext'; + public const Lower_Sorbian = 'dsb'; + public const Doteli = 'dty'; + public const Lingala = 'ln'; + public const Zamboanga_Chavacano = 'cbk-zam'; + public const Igbo = 'ig'; + public const Divehi = 'dv'; + public const Kinyarwanda = 'rw'; + public const Ripuarian = 'ksh'; + public const Gagauz = 'gag'; + public const Buryat_Russia = 'bxr'; + public const Palatinate_German = 'pfl'; + public const Avar = 'av'; + public const Pali = 'pi'; + public const Pangasinan = 'pag'; + public const Hawaiian = 'haw'; + public const Awadhi = 'awa'; + public const Atayal = 'tay'; + public const Papiamentu = 'pap'; + public const Karachay_Balkar = 'krc'; + public const Kalmyk = 'xal'; + public const Sakizaya = 'szy'; + public const Zhuang = 'za'; + public const Ingush = 'inh'; + public const Karakalpak = 'kaa'; + public const Pennsylvania_German = 'pdc'; + public const Atikamekw = 'atj'; + public const Tongan = 'to'; + public const Aramaic = 'arc'; + public const Kabiye = 'kbp'; + public const Tok_Pisin = 'tpi'; + public const Jamaican = 'jam'; + public const Nauruan = 'na'; + public const Wolof = 'wo'; + public const Twi = 'tw'; + public const Kabardian_Circassian = 'kbd'; + public const Moksha = 'mdf'; + public const Tulu = 'tcy'; + public const Dagbani = 'dag'; + public const Novial = 'nov'; + public const Kikuyu = 'ki'; + public const Nias = 'nia'; + public const Tetum = 'tet'; + public const Luganda = 'lg'; + public const Bislama = 'bi'; + public const Lojban = 'jbo'; + public const Aromanian = 'roa-rup'; + public const Kongo = 'kg'; + public const Fijian = 'fj'; + public const Xhosa = 'xh'; + public const Lak = 'lbe'; + public const Tahitian = 'ty'; + public const N_Ko = 'nqo'; + public const Tumbuka = 'tum'; + public const Mon = 'mnw'; + public const Old_Church_Slavonic = 'cu'; + public const Tachelhit = 'shi'; + public const Seediq = 'trv'; + public const Sranan = 'srn'; + public const Kashmiri = 'ks'; + public const Oromo = 'om'; + public const Samoan = 'sm'; + public const Guianan_Creole = 'gcr'; + public const Altai = 'alt'; + public const Latgalian = 'ltg'; + public const Cherokee = 'chr'; + public const Norfolk = 'pih'; + public const Chichewa = 'ny'; + public const Gothic = 'got'; + public const Madurese = 'mad'; + public const Sesotho = 'st'; + public const Amis = 'ami'; + public const Greenlandic = 'kl'; + public const Romani = 'rmy'; + public const Tswana = 'tn'; + public const Bambara = 'bm'; + public const Tsonga = 'ts'; + public const Cheyenne = 'chy'; + public const Venda = 've'; + public const Kirundi = 'rn'; + public const Inuktitut = 'iu'; + public const Akan = 'ak'; + public const Swati = 'ss'; + public const Chamorro = 'ch'; + public const Pontic = 'pnt'; + public const Adyghe = 'ady'; + public const Inupiak = 'ik'; + public const Ewe = 'ee'; + public const Fula = 'ff'; + public const Dinka = 'din'; + public const Sango = 'sg'; + public const Dzongkha = 'dz'; + public const Tigrinya = 'ti'; + public const Paiwan = 'pwn'; + public const Cree = 'cr'; + public const Ndonga = 'ng'; + public const Choctaw = 'cho'; + public const Marshallese = 'mh'; + public const Kuanyama = 'kj'; + public const Sichuan_Yi = 'ii'; + public const Hiri_Motu = 'ho'; + public const Afar = 'aa'; + public const Northern_Luri = 'lrc'; + public const Muscogee = 'mus'; + public const Herero = 'hz'; + public const Kanuri = 'kr'; } diff --git a/src/Phony.php b/src/Phony.php index c36ed7c..313ea3a 100644 --- a/src/Phony.php +++ b/src/Phony.php @@ -4,8 +4,8 @@ namespace Phonyland\Framework; -use Phonyland\GeneratorManager\Container; use RuntimeException; +use Phonyland\GeneratorManager\Container; /** * Class Phony. diff --git a/tests/Generator/MagicTest.php b/tests/Generator/MagicTest.php index ee1d958..d596990 100644 --- a/tests/Generator/MagicTest.php +++ b/tests/Generator/MagicTest.php @@ -60,25 +60,25 @@ expect($🙃->sampleOne->basicMethod())->toBe('simple data'); }); -it('can not access undefined magic attributes', function() { +it('can not access undefined magic attributes', function (): void { // @phpstan-ignore-next-line (new SampleOneGenerator('sampleOne', 'phonyland/sample-one-generator', 🙃()))->nonExistingAttribute; })->throws(RuntimeException::class); -it('can not set any magic attribute', function () { +it('can not set any magic attribute', function (): void { $generator = new SampleOneGenerator('sampleOne', 'phonyland/sample-one-generator', 🙃()); // @phpstan-ignore-next-line $generator->simpleAttribute = 'not-allowed'; })->throws(RuntimeException::class); -it('can check the existence of a magic attribute', function () { +it('can check the existence of a magic attribute', function (): void { $generator = new SampleOneGenerator('sampleOne', 'phonyland/sample-one-generator', 🙃()); expect(isset($generator->simpleAttribute))->toBeTrue(); expect(isset($generator->nonExistingAttribute))->toBeFalse(); }); -it('can not access undefined magic methods', function() { +it('can not access undefined magic methods', function (): void { // @phpstan-ignore-next-line (new SampleOneGenerator('sampleOne', 'phonyland/sample-one-generator', 🙃()))->nonExistingMethod(); })->throws(RuntimeException::class); diff --git a/tests/Pest.php b/tests/Pest.php index 4411b75..70ba775 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -2,26 +2,20 @@ declare(strict_types=1); -use Phonyland\Framework\Generator; -use Phonyland\Framework\Locale; use Phonyland\Framework\Phony; +use Phonyland\Framework\Locale; +use Phonyland\Framework\Generator; function 🙃( string $defaultLocale = Locale::English, - ?int $seed = null + int $seed = null ): Phony { return new Phony($defaultLocale, $seed); } /** - * @param string $generatorClass - * @param \Phonyland\Framework\Phony $phonyInstance - * @param string $alias - * @param string $packageName - * @param array $dataFilePaths - * @param array $methodNames - * @param bool $mockMethodCalls - * @param bool $mockBuildDataPath + * @param array $dataFilePaths + * @param array $methodNames * * @return array{0: Phony, 1: Generator} */ @@ -37,14 +31,14 @@ function fakeGeneratorWithData( ): array { /** @var \Mockery\MockInterface|Generator $generator */ $generator = Mockery::mock($generatorClass, [$alias, $packageName, $phonyInstance]) - ->shouldAllowMockingProtectedMethods() - ->makePartial(); + ->shouldAllowMockingProtectedMethods() + ->makePartial(); if ($mockMethodCalls === true) { foreach ($methodNames as $methodName) { $generator->shouldReceive($methodName) - ->once() - ->passthru(); + ->once() + ->passthru(); } } @@ -55,26 +49,26 @@ function fakeGeneratorWithData( ->shouldReceive('buildDataPath') ->once() ->with($dataFilePath) - ->andReturn(getcwd() . '/tests/Stubs/data/' . implode('/', $dataFilePath) . '.php'); + ->andReturn(getcwd().'/tests/Stubs/data/'.implode('/', $dataFilePath).'.php'); } } - $generator->setDataPackages(['en' => "phonyland-data-fake/$packageName"]); + $generator->setDataPackages(['en' => "phonyland-data-fake/{$packageName}"]); $phonyInstance->container->set($alias, $generator); return [$phonyInstance, $generator]; } -if (! function_exists('dd')) { +if (!function_exists('dd')) { function dd(): void { call_user_func_array('dump', func_get_args()); - die(); + exit(); } } -if (! function_exists('d')) { +if (!function_exists('d')) { function d(): void { call_user_func_array('dump', func_get_args()); diff --git a/tests/PipeTest.php b/tests/PipeTest.php index 4ada2e3..6c63a53 100644 --- a/tests/PipeTest.php +++ b/tests/PipeTest.php @@ -10,7 +10,7 @@ $callCount = random_int(2, 10); for ($i = 1; $i <= $callCount; $i++) { - $pipe(fn($value) => $value * 2); + $pipe(fn ($value) => $value * 2); } expect($pipe->value)->toBe(2 ** ($callCount + 1)); diff --git a/tests/Stubs/SampleOneGenerator.php b/tests/Stubs/SampleOneGenerator.php index 53dc58a..473cb35 100644 --- a/tests/Stubs/SampleOneGenerator.php +++ b/tests/Stubs/SampleOneGenerator.php @@ -12,16 +12,13 @@ class SampleOneGenerator extends Generator 'simpleAttribute' => 'sampleOne.simple', 'nestedSimpleAttribute' => 'sampleOne.nested.nested', ]; - protected array $attributeAliases = [ 'nestedAttributeAlias' => 'nestedSimpleAttribute', ]; - protected array $methodsAsAttributes = [ 'simple' => [], 'nestedSimple' => [], ]; - protected array $methodAliases = [ 'basicMethod' => 'simple', ];