Skip to content

Commit

Permalink
[10.x] Uses PHP Native Type Declarations 🐘 (#44545)
Browse files Browse the repository at this point in the history
* Adds types to `up` and `down` migration methods

* Adds types casts `get` and `set` methods

* Adds types to HTTP Authenticate `redirectTo` method

* Adds types to `handle` middleware methods

* Adds types to `join` channel method

* Adds types to `handle` command method

* Adds types to `render` component method

* Adds types to `broadcastOn` event method

* Improves types order

* Adds types to `definition` model factory method

* Adds types to `handle` job method

* Types missing `render` method in Exception

* Adds types to `handle` listener method

* Adds types to `render` and `report` Exception method

* Adds types to `build` Mail method

* Fixes missing param type in `handle` middleware method

* Adds types to notifications

* Adds types to observers

* Adds types to policies

* Adds types to providers

* Adds types to requests

* Adds types to resources

* Adds types to rules

* Adds missing types to rules

* Adds types to scopes

* Adds types to seeds

* Adds types to tests

* Adds types to Auth user providers

* Adds types to Authenticatable

* Adds types to `SerializesCastableAttributes`

* Adds missing type

* Migrates missing job stub

* Adds types to UrlRoutable

* Reverts changes on test migrations

* Reverts changes on test migrations

* Reverts changes on test migrations

* Reverts changes on `Authenticatable`

* Reverts changes on `UserProvider`

* Reverts changes on UserProvider

* Changes the console stub

* Fixes missing type annotation

* Reverts changes in docs

* Swaps order of params

* Fixes key type

* Reverts changes on UrlRoutable

* Reverts changes on UrlRoutable

* Types BroadcastsEvents

* Simplifies event stub

* Adds types to inbound stub

* Makes `report` void return type

* Makes `report` void return type

* Adds missing type on validaton

* Clarifies that `$notifiable` is an object

* Fixes type on "render" errors

* Avoids issues with Larastan

* Reverts

* Improves rules types

* Simplifies policy's return type

* Types controllers stubs

* Adds types to singleton stubs

* Fixes return type

* Use `RedirectResponse` on non api controllers (#45485)
  • Loading branch information
nunomaduro committed Jan 3, 2023
1 parent c152a97 commit eb04fb4
Show file tree
Hide file tree
Showing 66 changed files with 210 additions and 537 deletions.
3 changes: 2 additions & 1 deletion src/Illuminate/Auth/Middleware/Authenticate.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Auth\AuthenticationException;
use Illuminate\Contracts\Auth\Factory as Auth;
use Illuminate\Contracts\Auth\Middleware\AuthenticatesRequests;
use Illuminate\Http\Request;

class Authenticate implements AuthenticatesRequests
{
Expand Down Expand Up @@ -90,7 +91,7 @@ protected function unauthenticated($request, array $guards)
* @param \Illuminate\Http\Request $request
* @return string|null
*/
protected function redirectTo($request)
protected function redirectTo(Request $request)
{
//
}
Expand Down
8 changes: 2 additions & 6 deletions src/Illuminate/Cache/Console/stubs/cache.stub
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
public function up(): void
{
Schema::create('cache', function (Blueprint $table) {
$table->string('key')->primary();
Expand All @@ -28,10 +26,8 @@ return new class extends Migration

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
public function down(): void
{
Schema::dropIfExists('cache');
Schema::dropIfExists('cache_locks');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Illuminate\Contracts\Database\Eloquent;

use Illuminate\Database\Eloquent\Model;

interface CastsAttributes
{
/**
Expand All @@ -13,7 +15,7 @@ interface CastsAttributes
* @param array $attributes
* @return mixed
*/
public function get($model, string $key, $value, array $attributes);
public function get(Model $model, string $key, mixed $value, array $attributes);

/**
* Transform the attribute to its underlying model values.
Expand All @@ -24,5 +26,5 @@ public function get($model, string $key, $value, array $attributes);
* @param array $attributes
* @return mixed
*/
public function set($model, string $key, $value, array $attributes);
public function set(Model $model, string $key, mixed $value, array $attributes);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Illuminate\Contracts\Database\Eloquent;

use Illuminate\Database\Eloquent\Model;

interface CastsInboundAttributes
{
/**
Expand All @@ -13,5 +15,5 @@ interface CastsInboundAttributes
* @param array $attributes
* @return mixed
*/
public function set($model, string $key, $value, array $attributes);
public function set(Model $model, string $key, mixed $value, array $attributes);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Illuminate\Contracts\Database\Eloquent;

use Illuminate\Database\Eloquent\Model;

interface SerializesCastableAttributes
{
/**
Expand All @@ -13,5 +15,5 @@ interface SerializesCastableAttributes
* @param array $attributes
* @return mixed
*/
public function serialize($model, string $key, $value, array $attributes);
public function serialize(Model $model, string $key, mixed $value, array $attributes);
}
2 changes: 1 addition & 1 deletion src/Illuminate/Contracts/Validation/DataAwareRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ interface DataAwareRule
* @param array $data
* @return $this
*/
public function setData($data);
public function setData(array $data);
}
4 changes: 3 additions & 1 deletion src/Illuminate/Contracts/Validation/InvokableRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Illuminate\Contracts\Validation;

use Closure;

interface InvokableRule
{
/**
Expand All @@ -12,5 +14,5 @@ interface InvokableRule
* @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail
* @return void
*/
public function __invoke($attribute, $value, $fail);
public function __invoke(string $attribute, mixed $value, Closure $fail);
}
4 changes: 3 additions & 1 deletion src/Illuminate/Contracts/Validation/ValidatorAwareRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Illuminate\Contracts\Validation;

use Illuminate\Validation\Validator;

interface ValidatorAwareRule
{
/**
Expand All @@ -10,5 +12,5 @@ interface ValidatorAwareRule
* @param \Illuminate\Validation\Validator $validator
* @return $this
*/
public function setValidator($validator);
public function setValidator(Validator $validator);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class {{ factory }}Factory extends Factory
*
* @return array<string, mixed>
*/
public function definition()
public function definition(): array
{
return [
//
Expand Down
4 changes: 1 addition & 3 deletions src/Illuminate/Database/Console/Seeds/stubs/seeder.stub
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ class {{ class }} extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
public function run(): void
{
//
}
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Eloquent/BroadcastsEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public function newBroadcastableModelEvent($event)
* @param string $event
* @return \Illuminate\Database\Eloquent\BroadcastableModelEventOccurred
*/
protected function newBroadcastableEvent($event)
protected function newBroadcastableEvent(string $event)
{
return new BroadcastableModelEventOccurred($this, $event);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
public function up(): void
{
Schema::create('{{ table }}', function (Blueprint $table) {
$table->id();
Expand All @@ -21,10 +19,8 @@ return new class extends Migration

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
public function down(): void
{
Schema::dropIfExists('{{ table }}');
}
Expand Down
8 changes: 2 additions & 6 deletions src/Illuminate/Database/Migrations/stubs/migration.stub
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,16 @@ return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
public function up(): void
{
//
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
public function down(): void
{
//
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
public function up(): void
{
Schema::table('{{ table }}', function (Blueprint $table) {
//
Expand All @@ -20,10 +18,8 @@ return new class extends Migration

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
public function down(): void
{
Schema::table('{{ table }}', function (Blueprint $table) {
//
Expand Down
3 changes: 2 additions & 1 deletion src/Illuminate/Foundation/Auth/EmailVerificationRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Auth\Events\Verified;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Validator;

class EmailVerificationRequest extends FormRequest
{
Expand Down Expand Up @@ -57,7 +58,7 @@ public function fulfill()
* @param \Illuminate\Validation\Validator $validator
* @return void
*/
public function withValidator($validator)
public function withValidator(Validator $validator)
{
return $validator;
}
Expand Down
9 changes: 3 additions & 6 deletions src/Illuminate/Foundation/Console/stubs/cast.inbound.stub
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,17 @@

namespace {{ namespace }};

use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Database\Eloquent\CastsInboundAttributes;

class {{ class }} implements CastsInboundAttributes
{
/**
* Prepare the given value for storage.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param string $key
* @param mixed $value
* @param array $attributes
* @return mixed
* @param array<string, mixed> $attributes
*/
public function set($model, string $key, $value, array $attributes)
public function set(Model $model, string $key, mixed $value, array $attributes): mixed
{
return $value;
}
Expand Down
17 changes: 5 additions & 12 deletions src/Illuminate/Foundation/Console/stubs/cast.stub
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,27 @@

namespace {{ namespace }};

use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;

class {{ class }} implements CastsAttributes
{
/**
* Cast the given value.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param string $key
* @param mixed $value
* @param array $attributes
* @return mixed
* @param array<string, mixed> $attributes
*/
public function get($model, string $key, $value, array $attributes)
public function get(Model $model, string $key, mixed $value, array $attributes): mixed
{
return $value;
}

/**
* Prepare the given value for storage.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param string $key
* @param mixed $value
* @param array $attributes
* @return mixed
* @param array<string, mixed> $attributes
*/
public function set($model, string $key, $value, array $attributes)
public function set(Model $model, string $key, mixed $value, array $attributes): mixed
{
return $value;
}
Expand Down
7 changes: 1 addition & 6 deletions src/Illuminate/Foundation/Console/stubs/channel.stub
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ class {{ class }}
{
/**
* Create a new channel instance.
*
* @return void
*/
public function __construct()
{
Expand All @@ -18,11 +16,8 @@ class {{ class }}

/**
* Authenticate the user's access to the channel.
*
* @param \{{ namespacedUserModel }} $user
* @return array|bool
*/
public function join({{ userModel }} $user)
public function join({{ userModel }} $user): array|bool
{
//
}
Expand Down
6 changes: 2 additions & 4 deletions src/Illuminate/Foundation/Console/stubs/console.stub
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@ class {{ class }} extends Command

/**
* Execute the console command.
*
* @return int
*/
public function handle()
public function handle(): void
{
return Command::SUCCESS;
//
}
}
10 changes: 5 additions & 5 deletions src/Illuminate/Foundation/Console/stubs/event.stub
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ class {{ class }}

/**
* Create a new event instance.
*
* @return void
*/
public function __construct()
{
Expand All @@ -27,10 +25,12 @@ class {{ class }}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
* @return array<int, \Illuminate\Broadcasting\Channel>
*/
public function broadcastOn()
public function broadcastOn(): array
{
return new PrivateChannel('channel-name');
return [
new PrivateChannel('channel-name'),
];
}
}
Loading

0 comments on commit eb04fb4

Please sign in to comment.