Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]:PHP Parse error: syntax error, unexpected '->' (T_OBJECT_OPERATOR) #217

Open
hlxvi opened this issue Jan 22, 2025 · 2 comments
Open
Assignees
Labels

Comments

@hlxvi
Copy link

hlxvi commented Jan 22, 2025

Extension Version

0.1.18

PHP Binary

Local PHP

Operating System

Windows

What happened?

2025-01-22 18:50:28.173 [info] Checking herd PHP installation: herd which-php
2025-01-22 18:50:28.173 [info] Checking valet PHP installation: valet which-php
2025-01-22 18:50:28.173 [info] Checking local PHP installation: php -r 'echo PHP_BINARY;'
2025-01-22 18:50:28.173 [info] Checking sail PHP installation: ./vendor/bin/sail ps
2025-01-22 18:50:28.174 [info] Falling back to system PHP installation
2025-01-22 18:50:28.174 [error] Error:
Eloquent Attributes and Relations

PHP Parse error: syntax error, unexpected '->' (T_OBJECT_OPERATOR) in E:\laravel\vendor_laravel_ide\discover-de1f506da4a5c2d0d4253f2c83847947.php on line 61

2025-01-22 18:50:28.174 [error] php "e:\laravel\vendor_laravel_ide\discover-de1f506da4a5c2d0d4253f2c83847947.php"
2025-01-22 18:50:28.174 [error] Eloquent Attributes and Relations

PHP Parse error: syntax error, unexpected '->' (T_OBJECT_OPERATOR) in E:\laravel\vendor_laravel_ide\discover-de1f506da4a5c2d0d4253f2c83847947.php on line 61

2025-01-22 18:50:28.174 [info] Activating Laravel Extension...
2025-01-22 18:50:28.174 [info] Started

Mimimal Code Sample

<?php


error_reporting(E_ERROR | E_PARSE);

define('LARAVEL_START', microtime(true));

require_once __DIR__ . '/../autoload.php';
$app = require_once __DIR__ . '/../../bootstrap/app.php';

class VsCodeLaravel extends \Illuminate\Support\ServiceProvider
{
    public function register()
    {
    }

    public function boot()
    {
        config([
            'logging.channels.null' => [
                'driver' => 'monolog',
                'handler' => \Monolog\Handler\NullHandler::class,
            ],
            'logging.default' => 'null',
        ]);
    }
}

function vsCodeToRelativePath($path)
{
    if (!str_contains($path, base_path())) {
        return (string) $path;
    }

    return ltrim(str_replace(base_path(), '', realpath($path)), DIRECTORY_SEPARATOR);
}

$app->register(new VsCodeLaravel($app));
$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);
$kernel->bootstrap();

echo '__VSCODE_LARAVEL_START_OUTPUT__';

collect(glob(base_path('**/Models/*.php')))->each(fn ($file) => include_once($file));

if (class_exists('\phpDocumentor\Reflection\DocBlockFactory')) {
    $factory = \phpDocumentor\Reflection\DocBlockFactory::createInstance();
} else {
    $factory = null;
}

$reflection = new \ReflectionClass(\Illuminate\Database\Query\Builder::class);
$builderMethods = collect($reflection->getMethods(\ReflectionMethod::IS_PUBLIC))
    ->filter(function (ReflectionMethod $method) {
        return !str_starts_with($method->getName(), "__");
    })
  ->map(function (\ReflectionMethod $method) use ($factory) {
    if ($factory === null) {
         $params =  collect($method->getParameters())
             ->map(function (\ReflectionParameter $param) {
               $types = match ($param?->getType()) {
                 null => [],
                 default => method_exists($param->getType(), "getTypes")
                   ? $param->getType()->getTypes()
                   : [$param->getType()]
               };

               $types = collect($types)
                   ->filter()
                   ->values()
                   ->map(fn ($t) => $t->getName());

               return trim($types->join("|") . " $" . $param->getName());
             })
             ->all();

        $return = $method->getReturnType()?->getName();
    } else {
        $docblock = $factory->create($method->getDocComment());
        $params = collect($docblock->getTagsByName("param"))->map(fn($p) => (string) $p)->all();
        $return = (string) $docblock->getTagsByName("return")[0] ?? null;
    }

    return [
      "name" => $method->getName(),
      "parameters" => $params,
      "return" => $return,
    ];
  })
  ->filter()
  ->values();

echo collect([
'builderMethods' => $builderMethods,
'models' => collect(get_declared_classes())
    ->filter(function ($class) {
        return is_subclass_of($class, \Illuminate\Database\Eloquent\Model::class);
    })
    ->filter(function ($class) {
        return !in_array($class, [\Illuminate\Database\Eloquent\Relations\Pivot::class, \Illuminate\Foundation\Auth\User::class]);
    })
    ->values()
    ->flatMap(function (string $className) {
        $output = new \Symfony\Component\Console\Output\BufferedOutput();

        try {
            \Illuminate\Support\Facades\Artisan::call(
                "model:show",
                [
                    "model" => $className,
                    "--json" => true,
                ],
                $output
            );
        } catch (\Exception | \Throwable $e) {
            return null;
        }

        $data = json_decode($output->fetch(), true);

        if ($data === null) {
            return null;
        }

        $data['attributes'] = collect($data['attributes'])
            ->map(function ($attrs) {
                return array_merge($attrs, [
                    'title_case' => str_replace('_', '', \Illuminate\Support\Str::title($attrs['name'])),
                ]);
            })
            ->toArray();

        $reflection = (new \ReflectionClass($className));

        $data['scopes'] = collect($reflection->getMethods())
            ->filter(function ($method) {
                return $method->isPublic() && !$method->isStatic() && $method->name !== '__construct';
            })
            ->filter(function ($method) {
                return str_starts_with($method->name, 'scope');
            })
            ->map(function ($method) {
                return str_replace('scope', '', $method->name);
            })
            ->map(function ($method) {
                return strtolower(substr($method, 0, 1)) . substr($method, 1);
            })
            ->values()
            ->toArray();

        $data['uri'] = $reflection->getFileName();

        return [
            $className => $data,
        ];
    })
    ->filter(),
])->toJson();
;
echo '__VSCODE_LARAVEL_END_OUTPUT__';

exit(0);
@hlxvi hlxvi added the bug label Jan 22, 2025
@hlxvi hlxvi changed the title [Bug]: [Bug]:PHP Parse error: syntax error, unexpected '->' (T_OBJECT_OPERATOR) Jan 22, 2025
@joetannenbaum
Copy link
Collaborator

It looks like your local PHP installation may be below 8.0 as the null safe operator is causing an error in your script. Can you confirm your local PHP version?

@hlxvi
Copy link
Author

hlxvi commented Jan 24, 2025

It looks like your local PHP installation may be below 8.0 as the null safe operator is causing an error in your script. Can you confirm your local PHP version?

I am using PHP version 7.4.3,it is ok when i upgrade to 8.4.3,the extensions only support 8.0+ ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants