简体   繁体   中英

Composer not working after specifying platform php version

I am working on a project for a WebApp and I would like to specify types for my class properties. I didn't realise this is a PHP version 7.4.* feature, so I read this and updated my composer.json to include the relevant material:

{
    "name": "srmes/shopping-app-test",
    "description": "an assignment from `scandiweb.com`. A simple php-based web application to display and inventory a range of products",
    "require-dev":  {
        "phpunit/phpunit":"~9.0",
        "squizlabs/php_codesniffer": "~3.0"
    },
    "require": {
        "doctrine/orm": "~2.7",
        "php": "7.4.4"
    },
    "config": {
        "platform": {
            "php": "7.4.4"
        }
    },
    "autoload" : {
        "psr-4": {
            "WebApp\\": "src/"
         }
    },
    "autoload-dev" : {
        "psr-4": {
            "WebApp\\Tests\\" : "tests/"
        }
    }
}

I then ran composer install and composer update .

No problems seemed to occur with the install, except that now my phpunit tests don't run:

PHPUnit 9.1.1 by Sebastian Bergmann and contributors.



Time: 58 ms, Memory: 4.00 MB

No tests executed!

And all composer commands give the following error:

Parse error: syntax error, unexpected 'string' (T_STRING), expecting function (T_FUNCTION) or const (T_CONST) in /Users/ScottAnderson/Documents/Tech/commissions/shopping_app_test/vendor/ocramius/package-versions/src/PackageVersions/Installer.php on line 33

Ironically this is syntax exception about the php feature I was trying to utilise! I can't even run composer -vvv to debug which php executable is being used by composer.

In order to resolve this should I use a package like phpbrew to make directory environments of php?

My assumption was that after requiring php 7.4.4 in composer.json that the correct php executable would be installed and used by composer and phpunit

Looks like you are not running php 7.4. and the dependencies installed (here phpunit and PackageVersion) need it. Sometimes composer runs commands hooked on events and if the command fails everything else can fall.

  • Delete the vendor folder.
  • Delete composer.lock
  • Remove the constraint on php 7.4 version in your composer.json
  • Run composer update You should be alright

Btw composer manages project dependencies, not php versions installed. If you're starting out in PHP I'd recommend to stick with 7.3 which is widely available. Honestly you don't need the latest syntax additions to the language.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM