24

I have some syntax errors from symfony vendor when i'm trying to composer install my project.

Parse error: syntax error, unexpected '|', expecting variable (T_VARIABLE) vendor\psr\log\src\LoggerInterface.php on line 30

With several search, I saw the problem is from PHP version (have to be > 7.1), but actually my version is 7.4.9, this is a local machine, i'm working with WAMP on Windows. Symfony 4.4 version. Already tried to delete vendor, .lock...

Here the php version from phpinfo() PHP version from phpinfo()

Here php version from wamp PHP VERSION from wamp

Here php version from CLI php-v php version from php v

Here my composer.json

{
"type": "project",
"license": "proprietary",    
"require": {
    "php": ">=7.1.3",
    "ext-ctype": "*",
    "ext-iconv": "*",
    "doctrine/annotations": "^1.13",
    "mongodb/mongodb": "^1.10@dev",
    "ramsey/uuid": "^4.2",
    "symfony/console": "4.4.*",
    "symfony/dotenv": "4.4.*",
    "symfony/flex": "^1.3.1",
    "symfony/form": "4.4.*",
    "symfony/framework-bundle": "4.4.*",
    "symfony/messenger": "4.4.*",
    "symfony/yaml": "4.4.*"
},
"require-dev": {
},
"minimum-stability": "dev",
"config": {
    "preferred-install": {
        "*": "dist"
    },
    "sort-packages": true,
    "platform": {
        "php": "7.4.9"
    }
},
"autoload": {
    "psr-4": {
        "App\\": "src/"
    }
},
"autoload-dev": {
    "psr-4": {
        "App\\Tests\\": "tests/"
    }
},
"replace": {
    "paragonie/random_compat": "2.*",
    "symfony/polyfill-ctype": "*",
    "symfony/polyfill-iconv": "*",
    "symfony/polyfill-php71": "*",
    "symfony/polyfill-php70": "*",
    "symfony/polyfill-php56": "*"
},
"scripts": {
    "auto-scripts": {
        "cache:clear": "symfony-cmd",
        "assets:install %PUBLIC_DIR%": "symfony-cmd"
    },
    "post-install-cmd": [
        "@auto-scripts"
    ],
    "post-update-cmd": [
        "@auto-scripts"
    ]
},
"conflict": {
    "symfony/symfony": "*"
},
"extra": {
    "symfony": {
        "allow-contrib": false,
        "require": "4.4.*"
    }
}
}

Have some idea ?

akali leona
  • 403
  • 1
  • 3
  • 7
  • 2
    You seemingly have [latest version installed](https://packagist.org/packages/psr/log), which uses PHP/8 syntax (you can check that with e.g. `composer info psr/log`). Either it's ignoring your `"platform"` settings for some reason, of you made changes but didn't update. Do you have a `composer.lock` file coming from the repository? Does `composer update` change something? – Álvaro González Sep 06 '21 at 10:50
  • How does `vendor\psr\log\src\LoggerInterface.php on line 30` look like, it is not accepting several parameters obviously, so there is a version issue with either the package or php. – Anuga Sep 06 '21 at 11:31
  • Composer update would be helpful – ExpertWeblancer Jan 11 '23 at 21:20

7 Answers7

18

I added this in the composer.json and it worked.

"config": {
        "platform": {
            "php": "7.4.21"
         }
   }

The run. composer update

This downgraded psr/log to a lower version from 2.0 and resolved my issue.

  • It's worth noting that the settings on the question already include a similar `platform` settings. It's possible that the OP didn't refresh with `composer update` or they had another issue. – Álvaro González Jan 19 '22 at 08:56
  • 1
    Thx that saved my day. But note that you may have to re-install your libs (yust delete composer.lock and composer/installed.json and run `composer update`) to make this taking affect – Radon8472 Apr 04 '22 at 08:50
13

I got the solution.

uninstall composer using following command

sudo apt remove composer

sudo apt autoremove

switch to compatible php you are looking for using following command (if multiple php versions are running on your system)

sudo update-alternatives --config php 

then install composer using older method given on below link

https://getcomposer.org/download/

vishal-mote
  • 362
  • 2
  • 6
  • 22
8

This character '|' is used only in php version >= 8.0 You can update the php version

That worked for me :)

References: https://www.php.net/releases/8.0/en.php#union-types

Gilmar
  • 97
  • 1
  • 3
  • 1
    This was it! I globally had PHP 7.4 (used by phpunit), but running in IntelliJ used the PHP 8.1 runner. – oligofren Jun 30 '22 at 11:35
0

Delete the vendor folder, delete the composer.lock file, then run composer install. that worked for me

microsoftjulius
  • 300
  • 3
  • 10
0

I also faced this error when I upgrade ubuntu version from 20 to 22. I was using php 7.4 version. I tried to find the solution on google and mostly was saying to upgrade php version to 8. I updated to php 8 version but then again composer start throwing errors in Laravel project. Nothing helpful. So I uninstall php 8 and then reinstall composer and php 7.4. Everything start working fine. So in my conclusion. We can try these steps:

  1. Reinstall Php version
  2. Reinstall composer if you are using composer.
  3. Restart your apache or nginx server
Arun verma
  • 31
  • 7
0

This can be caused by using the wrong actual PHP version with the PHP-version stated in composer.

Stef Van Looveren
  • 302
  • 1
  • 5
  • 15
0

If the other solutions not does works, u can try this:

curl -sS https://getcomposer.org/installer | php
php composer.phar update
Bruno Ribeiro
  • 1,280
  • 16
  • 21