From 650c24f8450b99d12e7d93d4e18da4934ffcddc3 Mon Sep 17 00:00:00 2001 From: AnuzPandey Date: Sun, 19 Nov 2023 02:21:32 +0545 Subject: [PATCH] :hammer: fix: memory exhaustion issue with recursive call. --- src/Traits/EnglishDateTrait.php | 8 +--- src/Traits/NepaliDateTrait.php | 39 +++++++--------- tests/ArchTest.php | 8 ++++ tests/ConvertToEnglishDateTest.php | 71 ++++++++++++------------------ tests/ConvertToNepaliDateTest.php | 70 ++++++++++++----------------- 5 files changed, 81 insertions(+), 115 deletions(-) diff --git a/src/Traits/EnglishDateTrait.php b/src/Traits/EnglishDateTrait.php index 24c8062..d85c358 100644 --- a/src/Traits/EnglishDateTrait.php +++ b/src/Traits/EnglishDateTrait.php @@ -64,10 +64,6 @@ trait EnglishDateTrait public function toEnglishDate(string $format = 'Y-m-d', string $locale = 'en'): string { - if ($format) { - return $this->toFormattedEnglishDate($format, $locale); - } - $checkIfIsInRange = $this->isInNepaliDateRange($this->date); if (!$checkIfIsInRange) { @@ -78,14 +74,12 @@ public function toEnglishDate(string $format = 'Y-m-d', string $locale = 'en'): $this->performCalculationBasedonNepaliDays($totalNepaliDays); - return $this->englishYear . '-' . $this->englishMonth . '-' . $this->englishDay; + return $this->toFormattedEnglishDate($format, $locale); } public function toEnglishDateArray(): NepaliDateArrayData { - $this->toEnglishDate(); - return NepaliDateArrayData::from([ 'year' => $this->englishYear, 'month' => $this->englishMonth, diff --git a/src/Traits/NepaliDateTrait.php b/src/Traits/NepaliDateTrait.php index 071d5ea..c71bbc8 100644 --- a/src/Traits/NepaliDateTrait.php +++ b/src/Traits/NepaliDateTrait.php @@ -4,7 +4,6 @@ use Anuzpandey\LaravelNepaliDate\DataTransferObject\NepaliDateArrayData; use Carbon\Carbon; -use Illuminate\Support\Str; use RuntimeException; trait NepaliDateTrait @@ -63,27 +62,11 @@ trait NepaliDateTrait ]; - public function toNepaliDate(string $format = 'Y-m-d', string $locale = 'np'): string + public function toNepaliDate(string $format = 'Y-m-d', string $locale = 'en'): string { - if ($format) { - return $this->toFormattedNepaliDate($format, $locale); - } - - $checkIfIsInRange = $this->isInEnglishDateRange($this->date); - - if (!$checkIfIsInRange) { - throw new RuntimeException($checkIfIsInRange); - } + $this->performCalculationOnEnglishDate(); - $totalEnglishDays = $this->calculateTotalEnglishDays($this->date->year, $this->date->month, $this->date->day); - - $this->performCalculationBasedOn($totalEnglishDays); - - $year = $this->nepaliYear; - $month = $this->nepaliMonth < 10 ? '0' . $this->nepaliMonth : $this->nepaliMonth; - $day = $this->nepaliDay < 10 ? '0' . $this->nepaliDay : $this->nepaliDay; - - return $year . '-' . $month . '-' . $day; + return $this->toFormattedNepaliDate($format, $locale); } @@ -100,8 +83,6 @@ public function toFormattedNepaliDate( public function toNepaliDateArray(): NepaliDateArrayData { - $this->toNepaliDate(); - $nepaliMonth = $this->nepaliMonth > 9 ? $this->nepaliMonth : '0' . $this->nepaliMonth; $nepaliDay = $this->nepaliDay > 9 ? $this->nepaliDay : '0' . $this->nepaliDay; @@ -146,6 +127,20 @@ public function getShortDayName(string $npDayName, string $locale = 'np'): strin } + public function performCalculationOnEnglishDate(): void + { + $checkIfIsInRange = $this->isInEnglishDateRange($this->date); + + if (!$checkIfIsInRange) { + throw new RuntimeException($checkIfIsInRange); + } + + $totalEnglishDays = $this->calculateTotalEnglishDays($this->date->year, $this->date->month, $this->date->day); + + $this->performCalculationBasedOn($totalEnglishDays); + } + + private function calculateTotalEnglishDays($year, $month, $day) { $totalEnglishDays = 0; diff --git a/tests/ArchTest.php b/tests/ArchTest.php index ccc19b2..2ac6385 100644 --- a/tests/ArchTest.php +++ b/tests/ArchTest.php @@ -3,3 +3,11 @@ it('will not use debugging functions') ->expect(['dd', 'dump', 'ray']) ->each->not->toBeUsed(); + +it('ensures traits are suffixed with Trait') + ->expect('Anuzpandey\LaravelNepaliDate\Traits') + ->toHaveSuffix('Trait'); + +it('ensures mixins are suffixed with Mixin') + ->expect('Anuzpandey\LaravelNepaliDate\Mixin') + ->toHaveSuffix('Mixin'); diff --git a/tests/ConvertToEnglishDateTest.php b/tests/ConvertToEnglishDateTest.php index 01441d3..d7694a1 100644 --- a/tests/ConvertToEnglishDateTest.php +++ b/tests/ConvertToEnglishDateTest.php @@ -2,46 +2,31 @@ use Anuzpandey\LaravelNepaliDate\LaravelNepaliDate; -//it('can convert to basic nepali date', function (string $date, string $expectedResult) { -// $nepaliDate = LaravelNepaliDate::from($date)->toEnglishDate(); -// -// expect($nepaliDate) -// ->toBe($expectedResult); -//})->with([ -// ['2053-01-10', '1996-04-22'], -// ['2029-04-04', '1972-07-19'], -// ['2022-12-20', '1966-04-02'], -//]); -// -//it('can convert to english date array', function () { -// $date = '2053-01-10'; -// -// $englishDateArray = LaravelNepaliDate::from($date)->toEnglishDateArray(); -// -// expect($englishDateArray->toArray()) -// ->toBeArray() -// ->toMatchArray([ -// 'year' => '1996', -// 'month' => '04', -// 'day' => '22', -// 'npYear' => '१९९६', -// 'npDayName' => 'सोमबार', -// ]); -//}); -// -//it('can convert to nepali formatted result', function (string $format, string $locale, string $expectedResult) { -// $date = '2053-01-10'; -// -// expect(LaravelNepaliDate::from($date)->toFormattedEnglishDate(format: $format, locale: $locale)) -// ->toBe($expectedResult); -//})->with([ -// ['d F Y, l', 'np', '२२ अप्रिल १९९६, सोमबार'], -// ['d F Y, l', 'en', '22 April 1996, Monday'], -// ['Y-m-d', 'np', '१९९६-०४-२२'], -// ['Y-m-d', 'en', '1996-04-22'], -// ['l, d F Y', 'np', 'सोमबार, २२ अप्रिल १९९६'], -// ['l, d F Y', 'en', 'Monday, 22 April 1996'], -// ['d F Y', 'np', '२२ अप्रिल १९९६'], -// ['d F Y', 'en', '22 April 1996'], -// ['Y/m/d', 'np', '१९९६/०४/२२'], -//]); +it('can convert to basic nepali date', function (string $date, string $expectedResult) { + $nepaliDate = LaravelNepaliDate::from($date)->toEnglishDate(); + + expect($nepaliDate) + ->toBe($expectedResult); +})->with([ + ['2053-01-10', '1996-04-22'], + ['2029-04-04', '1972-07-19'], + ['2022-12-20', '1966-04-02'], +]); + + +it('can convert to nepali formatted result', function (string $format, string $locale, string $expectedResult) { + $date = '2053-01-10'; + + expect(LaravelNepaliDate::from($date)->toEnglishDate(format: $format, locale: $locale)) + ->toBe($expectedResult); +})->with([ + ['d F Y, l', 'np', '२२ अप्रिल १९९६, सोमबार'], + ['d F Y, l', 'en', '22 April 1996, Monday'], + ['Y-m-d', 'np', '१९९६-०४-२२'], + ['Y-m-d', 'en', '1996-04-22'], + ['l, d F Y', 'np', 'सोमबार, २२ अप्रिल १९९६'], + ['l, d F Y', 'en', 'Monday, 22 April 1996'], + ['d F Y', 'np', '२२ अप्रिल १९९६'], + ['d F Y', 'en', '22 April 1996'], + ['Y/m/d', 'np', '१९९६/०४/२२'], +]); diff --git a/tests/ConvertToNepaliDateTest.php b/tests/ConvertToNepaliDateTest.php index 91b54ff..cd517fb 100644 --- a/tests/ConvertToNepaliDateTest.php +++ b/tests/ConvertToNepaliDateTest.php @@ -2,46 +2,30 @@ use Anuzpandey\LaravelNepaliDate\LaravelNepaliDate; -//it('can convert to basic nepali date', function (string $date, string $expectedResult) { -// $nepaliDate = LaravelNepaliDate::from($date)->toNepaliDate(); -// -// expect($nepaliDate) -// ->toBe($expectedResult); -//})->with([ -// ['1996-04-22', '2053-01-10'], -// ['1972-07-19', '2029-04-04'], -// ['1966-04-02', '2022-12-20'], -//]); -// -//it('can convert to nepali date array', function () { -// $date = '1996-04-22'; -// -// $nepaliDateArray = LaravelNepaliDate::from($date)->toNepaliDateArray(); -// -// expect($nepaliDateArray->toArray()) -// ->toBeArray() -// ->toMatchArray([ -// 'year' => '2053', -// 'month' => '01', -// 'day' => '10', -// 'npYear' => '२०५३', -// 'npDayName' => 'सोमबार', -// ]); -//}); -// -//it('can convert to nepali formatted result', function (string $format, string $locale, string $expectedResult) { -// $date = '1996-04-22'; -// -// expect(LaravelNepaliDate::from($date)->toFormattedNepaliDate(format: $format, locale: $locale)) -// ->toBe($expectedResult); -//})->with([ -// ['d F Y, l', 'np', '१० वैशाख २०५३, सोमबार'], -// ['d F Y, l', 'en', '10 Baisakh 2053, Monday'], -// ['Y-m-d', 'np', '२०५३-०१-१०'], -// ['Y-m-d', 'en', '2053-01-10'], -// ['l, d F Y', 'np', 'सोमबार, १० वैशाख २०५३'], -// ['l, d F Y', 'en', 'Monday, 10 Baisakh 2053'], -// ['d F Y', 'np', '१० वैशाख २०५३'], -// ['d F Y', 'en', '10 Baisakh 2053'], -// ['Y/m/d', 'np', '२०५३/०१/१०'], -//]); +it('can convert to basic nepali date', function (string $date, string $expectedResult) { + $nepaliDate = LaravelNepaliDate::from($date)->toNepaliDate(); + + expect($nepaliDate) + ->toBe($expectedResult); +})->with([ + ['1996-04-22', '2053-01-10'], + ['1972-07-19', '2029-04-04'], + ['1966-04-02', '2022-12-20'], +]); + +it('can convert to nepali formatted result', function (string $format, string $locale, string $expectedResult) { + $date = '1996-04-22'; + + expect(LaravelNepaliDate::from($date)->toNepaliDate(format: $format, locale: $locale)) + ->toBe($expectedResult); +})->with([ + ['d F Y, l', 'np', '१० वैशाख २०५३, सोमबार'], + ['d F Y, l', 'en', '10 Baisakh 2053, Monday'], + ['Y-m-d', 'np', '२०५३-०१-१०'], + ['Y-m-d', 'en', '2053-01-10'], + ['l, d F Y', 'np', 'सोमबार, १० वैशाख २०५३'], + ['l, d F Y', 'en', 'Monday, 10 Baisakh 2053'], + ['d F Y', 'np', '१० वैशाख २०५३'], + ['d F Y', 'en', '10 Baisakh 2053'], + ['Y/m/d', 'np', '२०५३/०१/१०'], +]);