Skip to content

Return types on methods. #401

@mikevandiepen

Description

@mikevandiepen

Synopsis:

Can we have return types for methods? e.g controller and model methods. It should be an easy implementation
since the docblock already has the returntype hinted.

Expected Behavior:

When generating from the .draft blueprint will add a return type to medhod's. This (should) also be a config option
because there can be people running on <7.0 versions of php. (which seems unlinkely regarding laravel EOL).

Example code::

I've added the return type to the stubs and created a configuration flag in case this issue will be handled.

config/blueprint.php

   /*
    |--------------------------------------------------------------------------
    | Method Return TypeHinting
    |--------------------------------------------------------------------------
    |
    | Enable or disable method return typehinting for blueprint generated
    | methods. Enabling this will enforce code strictness which increases
    | readability of code and will lower maintenance cost. This will only
    | Work for projects running PHP v7.0 or higher.
    | 
    */
    'method_return_types' => false

stubs/controller.method.stub

    /**
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function {{ method }}(Request $request): \Illuminate\Http\Response
    {
        {{ body }}
    }

stubs/factory.stub

<?php

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
use {{ namespacedModel }};

class {{ model }}Factory extends Factory
{
    /**
     * The name of the factory's corresponding model.
     *
     * @var string
     */
    protected $model = {{ model }}::class;

    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition(): array
    {
        return [
            //
        ];
    }
}

stubs/job.stub

<?php

namespace {{ namespace }};

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class {{ class }} implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    {{ properties }}

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle(): void
    {
        //
    }
}

stubs/mail.stub

<?php

namespace {{ namespace }};

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class {{ class }} extends Mailable
{
    use Queueable, SerializesModels;

    {{ properties }}

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build(): {{ class }}
    {
        return $this->view('view.name');
    }
}

stubs/migration.stub

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class {{ class }} extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up(): void
    {
        Schema::create('{{ table }}', function (Blueprint $table) {
            {{ definition }}
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down(): void
    {
        Schema::dropIfExists('{{ table }}');
    }
}

stubs/model.method.stub

    public function {{ method }}(): {{ namespacedReturnClass }}
    {
        return null;
    }

stubs/notification.stub

<?php

namespace {{ namespace }};

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Queue\SerializesModels;

class {{ class }} extends Notification
{
    use Queueable, SerializesModels;

    {{ properties }}

    /**
     * Get the notification's delivery channels.
     *
     * @param mixed $notifiable
     * @return array
     */
    public function via($notifiable): array
    {
        return ['mail'];
    }

    /**
     * Get the mail representation of the notification.
     *
     * @param mixed $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable): \Illuminate\Notifications\Messages\MailMessage
    {
        return (new MailMessage)
            ->line('The introduction to the notification.')
            ->action('Notification Action', 'https://laravel.com')
            ->line('Thank you for using our application!');
    }

    /**
     * Get the array representation of the notification.
     *
     * @param mixed $notifiable
     * @return array
     */
    public function toArray($notifiable): array
    {
        return [
            //
        ];
    }
}

stubs/request.stub

<?php

namespace {{ namespace }};

use Illuminate\Foundation\Http\FormRequest;

class {{ class }} extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize(): bool
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules(): array
    {
        return [
            {{ rules }}
        ];
    }
}

stubs/resource.stub

<?php

namespace {{ namespace }};

use {{ import }};

class {{ class }} extends {{ parentClass }}
{
    /**
     * Transform the {{ resource }} into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request): array
    {
        {{ body }}
    }
}

stubs/seeder.no-factory.stub

<?php

use Illuminate\Database\Seeder;

class {{ class }} extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run(): void
    {
        {{ body }}
    }
}

stubs/seeder.stub

<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;

class {{ class }} extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run(): void
    {
        //
    }
}

stubs/test.case.stub

    /**
     * @test
     */
    public function {{ method }}(): void
    {
        {{ body }}
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions