Skip to content

Commit

Permalink
Merge pull request #15 from EriBloo/master
Browse files Browse the repository at this point in the history
Fix Issue #12 + Add Issue #4 + cleanup repo
  • Loading branch information
mohammedmanssour authored May 10, 2024
2 parents ee69d0b + 0366995 commit 293ec10
Show file tree
Hide file tree
Showing 21 changed files with 261 additions and 62 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
"illuminate/contracts": "^10.0"
},
"require-dev": {
"larastan/larastan": "^2.9",
"laravel/pint": "^1.0",
"nunomaduro/collision": "^7.9",
"nunomaduro/larastan": "^2.0.1",
"orchestra/testbench": "^8.0",
"phpstan/extension-installer": "^1.1",
"phpstan/phpstan-deprecation-rules": "^1.0",
Expand All @@ -42,7 +42,7 @@
},
"scripts": {
"post-autoload-dump": "@php ./vendor/bin/testbench package:discover --ansi",
"analyse": "vendor/bin/phpstan analyse",
"analyse": "php -d memory_limit=2G vendor/bin/phpstan analyse",
"test": "vendor/bin/phpunit",
"test-coverage": "vendor/bin/phpunit --coverage",
"format": "vendor/bin/pint"
Expand Down
9 changes: 6 additions & 3 deletions database/factories/RepetitionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
use MohammedManssour\LaravelRecurringModels\Enums\RepetitionType;
use MohammedManssour\LaravelRecurringModels\Models\Repetition;

/**
* @extends Factory<Repetition>
*/
class RepetitionFactory extends Factory
{
protected $model = Repetition::class;
Expand All @@ -18,7 +21,7 @@ public function definition()
'repeatable_id' => null,
'repeatable_type' => null,
'type' => RepetitionType::Simple,
'start_at' => Carbon::createFromDate(fake()->dateTime())->startOfHour(),
'start_at' => Carbon::make(fake()->dateTime())->startOfHour(),
'interval' => $this->toSeconds(fake()->numberBetween(1, 30)),
'year' => null,
'month' => null,
Expand All @@ -33,7 +36,7 @@ public function definition()
public function morphs(Model $model): static
{
return $this->state([
'repeatable_id' => $model->id,
'repeatable_id' => $model->getKey(),
'repeatable_type' => $model->getMorphClass(),
]);
}
Expand All @@ -51,7 +54,7 @@ public function complex(string $year = '*', string $month = '*', string $day = '
]);
}

public function interval($days = null): static
public function interval(?int $days = null): static
{
return $this->state([
'interval' => $this->toSeconds($days ?? fake()->numberBetween(1, 30)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

return new class extends Migration
{
public function up()
public function up(): void
{
if (Schema::hasTable('repetitions')) {
return;
Expand All @@ -28,7 +28,7 @@ public function up()
});
}

public function down()
public function down(): void
{
Schema::dropIfExists('repetitions');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

return new class extends Migration
{
public function up()
public function up(): void
{
Schema::table('repetitions', function (Blueprint $table) {
$table->integer('tz_offset')->default(0)->after('start_at');
});
}

public function down()
public function down(): void
{
Schema::table('repetitions', function (Blueprint $table) {
$table->dropColumn('tz_offset');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

return new class extends Migration
{
public function up()
public function up(): void
{
Schema::table('repetitions', function (Blueprint $table) {
$table->string('week_of_month')->nullable()->after('week');
});
}

public function down()
public function down(): void
{
Schema::table('repetitions', function (Blueprint $table) {
$table->dropColumn('week_of_month');
Expand Down
3 changes: 1 addition & 2 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ includes:
- phpstan-baseline.neon

parameters:
level: 4
level: 6
paths:
- src
- config
- database
tmpDir: build/phpstan
checkOctaneCompatibility: true
Expand Down
4 changes: 2 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
</directory>
</testsuite>
</testsuites>
<coverage>
<!-- <coverage>
<report>
<html outputDirectory="build/coverage" />
<text outputFile="build/coverage.txt" />
<clover outputFile="build/logs/clover.xml" />
</report>
</coverage>
</coverage> -->
<logging>
<junit outputFile="build/report.junit.xml" />
</logging>
Expand Down
11 changes: 9 additions & 2 deletions src/Concerns/Repeatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,20 @@
use MohammedManssour\LaravelRecurringModels\Enums\RepetitionType;
use MohammedManssour\LaravelRecurringModels\Models\Repetition;
use MohammedManssour\LaravelRecurringModels\Support\Repeat;
use MohammedManssour\LaravelRecurringModels\Support\RepeatCollection;

/**
* @property-read \Illuminate\Support\Collection<integer, \Modules\RecurringEvents\Entities\Repeat> $repeats
* @property-read RepeatCollection $repetitions
*
* @method Builder whereOccurresOn(Carbon $date)
* @method Builder whereOccurresBetween(Carbon $start, Carbon $end)
*/
trait Repeatable
{
/*-----------------------------------------------------
* Relations
-----------------------------------------------------*/
/** @return MorphMany<Repetition> */
public function repetitions(): MorphMany
{
return $this->morphMany(Repetition::class, 'repeatable');
Expand All @@ -28,7 +33,7 @@ public function repetitions(): MorphMany
/**
* define the base date that we would use to calculate repetition start_at
*/
public function repetitionBaseDate(RepetitionType $type = null): Carbon
public function repetitionBaseDate(?RepetitionType $type = null): Carbon
{
return $this->created_at;
}
Expand All @@ -44,6 +49,7 @@ public function repeat(): Repeat
/*-----------------------------------------------------
* Scopes
-----------------------------------------------------*/
/** @param Builder<self> $query */
public function scopeWhereOccurresOn(Builder $query, Carbon $date): Builder
{
return $query->whereHas(
Expand All @@ -52,6 +58,7 @@ public function scopeWhereOccurresOn(Builder $query, Carbon $date): Builder
);
}

/** @param Builder<self> $query */
public function scopeWhereOccurresBetween(Builder $query, Carbon $start, Carbon $end): Builder
{
return $query->whereHas(
Expand Down
10 changes: 9 additions & 1 deletion src/Contracts/Repeatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,21 @@
use Carbon\CarbonInterface as Carbon;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use MohammedManssour\LaravelRecurringModels\Enums\RepetitionType;
use MohammedManssour\LaravelRecurringModels\Models\Repetition;
use MohammedManssour\LaravelRecurringModels\Support\Repeat;
use MohammedManssour\LaravelRecurringModels\Support\RepeatCollection;

/**
* @property-read RepeatCollection $repetitions
*
* @method MorphMany<int, RepeatCollection> repetitions()
*/
interface Repeatable
{
/** @return MorphMany<Repetition> */
public function repetitions(): MorphMany;

public function repetitionBaseDate(RepetitionType $type = null): Carbon;
public function repetitionBaseDate(?RepetitionType $type = null): Carbon;

public function repeat(): Repeat;
}
2 changes: 1 addition & 1 deletion src/Exceptions/DriverNotSupportedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class DriverNotSupportedException extends \Exception
{
public function __construct(string $driver, int $code = 0, Throwable $previous = null)
public function __construct(string $driver, int $code = 0, ?Throwable $previous = null)
{
parent::__construct("Database driver \"{$driver}\" is not supported.", $code, $previous);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class RepetitionEndsAfterNotAvailableException extends \Exception
{
public function __construct(int $code = 0, Throwable $previous = null)
public function __construct(int $code = 0, ?Throwable $previous = null)
{
parent::__construct('endsAfter method is not available for complex repetitions. Please use endsAt method instead to explicitly set end date.', $code, $previous);
}
Expand Down
Loading

0 comments on commit 293ec10

Please sign in to comment.