Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Parse error: syntax error, unexpected ':', expecting ';' or '{'

Tags:

php

php-7

phpunit

I'm try playing with php7. I have installed https://github.com/rlerdorf/php7dev and connected through phpstorm. I'm trying use new feature like this:

<?php
namespace Kata;

class StringCalculator
{
    public function add(string $parameters): int {
        return 0;
    }
}

then I try to test it like this:

<?php
namespace Kata;

class StringCalculatorTest extends \PHPUnit_Framework_TestCase
{
    public function testAddEmptyString()
    {
        $calc = new StringCalculator();
        $this->assertSame(0, $calc->add(''));
    }
}

and I launch with phpunit, unfortunately I have

PHP Parse error: syntax error, unexpected ':', expecting ';' or '{'

Probably I haven't installed php7 properly but it seems to be ok when I php -v

PHP 7.0.0-dev (cli) (built: May 25 2015 16:34:33) (DEBUG) Copyright (c) 1997-2015 The PHP Group Zend Engine v3.0.0-dev, Copyright (c) 1998-2015 Zend Technologies with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2015, by Zend Technologies

UPDATE

Problem is not in installation/configuration php7 (I think), because when I run it from cli like this:

<?php
    $calc = new Kata\StringCalculator();
    var_dump($calc->add(''));

outputs int(0) and no error.

So maybe the problem is in phpunit?

like image 548
dwr Avatar asked Nov 09 '22 04:11

dwr


1 Answers

From the error it is clear that there's a problem executing your code.

PHP Parse error: syntax error, unexpected ':', expecting ';' or '{'

The problem in your case is most likely coming from PHPUnit since you have a correct php 7 setup. If PHPUnit fails to understand your code it's gonna fail in the first bit of PHP 7 code which in your case using a return type declaration in this line

public function add(string $parameters): int {

and it's gonna be triggered in your phpunit test when you try to assert. If you comment your assersion the error should disappear.

I suggest verifiying the version of PHPUnit you are using, you should use version 5 or higher. (preferably 6 for php 7.0) For more details about PHPunit versions, checkout this link.

like image 111
Mohamed Salem Lamiri Avatar answered Nov 14 '22 22:11

Mohamed Salem Lamiri