Click here to Skip to main content
15,850,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I was running a project from github getting the error "Parse error: syntax error, unexpected identifier "string", expecting variable" it uses sebastianbergmann library. please help to solve the issue.



<?php declare(strict_types=1); 
namespace SebastianBergmann;/

use function end;
use function explode;
use function fclose;
use function is_dir;
use function is_resource;
use function proc_close;
use function proc_open;
use function stream_get_contents;
use function substr_count;
use function trim;

final class Version
{
    private readonly string $version;

    public function __construct(string $release, string $path)
    {
        $this->version = $this->generate($release, $path);
    }

    public function asString(): string
    {
        return $this->version;
    }

    private function generate(string $release, string $path): string
    {
        if (substr_count($release, '.') + 1 === 3) {
            $version = $release;
        } else {
            $version = $release . '-dev';
        }

        $git = $this->getGitInformation($path);

        if (!$git) {
            return $version;
        }

        if (substr_count($release, '.') + 1 === 3) {
            return $git;
        }

        $git = explode('-', $git);

        return $release . '-' . end($git);
    }

    private function getGitInformation(string $path): bool|string
    {
        if (!is_dir($path . DIRECTORY_SEPARATOR . '.git')) {
            return false;
        }

        $process = proc_open(
            'git describe --tags',
            [
                1 => ['pipe', 'w'],
                2 => ['pipe', 'w'],
            ],
            $pipes,
            $path
        );

        if (!is_resource($process)) {
            return false;
        }

        $result = trim(stream_get_contents($pipes[1]));

        fclose($pipes[1]);
        fclose($pipes[2]);

        $returnCode = proc_close($process);

        if ($returnCode !== 0) {
            return false;
        }

        return $result;
    }
}


What I have tried:

I tried deleting readonly from
private readonly string $version;
.
Posted
Updated 24-Jun-23 2:33am

Quote:
I tried deleting readonly from
- You cannot just delete it as it needs to be defined.

The best solution is Sandeep's solution as most servers now require php 8.> version code anyways, but if you want to delete the deprecated code, you can use -
final class Version
{
    private string $version;

    // Rest of the code...
}


The 'sebastianbergmann library' has been uploaded in the past few months meaning newest version numbers used. See more on the readonly properties in PHP 8.1 at PHP Readonly Properties[^]
 
Share this answer
 
v2
It mostly should be related to the version of the PHP you are using.

Believe you are on an older version as readonly was added in PHP v8.1[^].

Just make sure you are using the right version of PHP and the error should go away.
 
Share this answer
 
Comments
Andre Oosthuizen 24-Jun-23 7:30am    
Spot on!
Kunal Pradhan 2023 25-Jun-23 3:16am    
so what should i write instead of 'private readonly string $version;'.
Sandeep Mewara 25-Jun-23 4:13am    
As mentioned below, if you dont want to upgrade then replace that with:
final class Version
{
private string $version;

// Rest of the code...
}
Kunal Pradhan 2023 3-Jul-23 10:30am    
getting more errors, can you please tell me how to upgrade the php version in the poject.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900