Skip to content

Parsing a Class doesn't includes method body. #59

Description

@DarkGhostHunter

Version: 3.3.4

Bug Description

Using an existing class won't register method bodies at all.

Steps To Reproduce

Create one class with a method body, and then parse it manually.

<?php

namespace App\Providers;

use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Event;

class EventServiceProvider extends ServiceProvider
{
    /**
     * The event listener mappings for the application.
     *
     * @var array
     */
    protected $listen = [
        Registered::class => [
            SendEmailVerificationNotification::class,
        ],
    ];

    /**
     * Register any events for your application.
     *
     * @return void
     */
    public function boot()
    {
        parent::boot();

        //
    }
}
use App\Providers\EventServiceProvider;

$class = \Nette\PhpGenerator\ClassType::from(EventServiceProvider::class);

echo $class->getMethod('boot')->getBody(); // ''

Expected Behavior

use App\Providers\EventServiceProvider;

$class = \Nette\PhpGenerator\ClassType::from(EventServiceProvider::class);

echo $class->getMethod('boot')->getBody(); //  'parent::boot();\n\n//'

Possible Solution

Add this method to retrieve the body inside the \PhpGenerator\Factory::fromMethodReflection:

protected function getMethodBody(\ReflectionMethod $from) : string
{
    $lines = file($from->getFileName());
    $string = '';

    for ($start = $from->getStartLine()+1, $end = $from->getEndLine()-1; $start < $end; $start++) {
        $string .= $lines[$start];
    }

    return $string;
}

From there, the only thing would be to "unpad" each line.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions