Parse error: syntax error, unexpected '?', expecting identifier (T_STRING)

0

I can not find the error in this file, debug shows me error on line 46, but can not find a solution, some light?

<?php

/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <[email protected]>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Symfony\Component\CssSelector\XPath\Extension;

use Symfony\Component\CssSelector\XPath\Translator;
use Symfony\Component\CssSelector\XPath\XPathExpr;

/**
 * XPath expression translator attribute extension.
 *
 * This component is a port of the Python cssselect library,
 * which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
 *
 * @author Jean-François Simon <[email protected]>
 *
 * @internal
 */
class AttributeMatchingExtension extends AbstractExtension
{
    /**
     * {@inheritdoc}
     */
    public function getAttributeMatchingTranslators()
    {
        return array(
            'exists' => array($this, 'translateExists'),
            '=' => array($this, 'translateEquals'),
            '~=' => array($this, 'translateIncludes'),
            '|=' => array($this, 'translateDashMatch'),
            '^=' => array($this, 'translatePrefixMatch'),
            '$=' => array($this, 'translateSuffixMatch'),
            '*=' => array($this, 'translateSubstringMatch'),
            '!=' => array($this, 'translateDifferent'),
        );
    }

    public function translateExists(XPathExpr $xpath, string $attribute, ?string $value): XPathExpr
    {
        return $xpath->addCondition($attribute);
    }

    public function translateEquals(XPathExpr $xpath, string $attribute, ?string $value): XPathExpr
    {
        return $xpath->addCondition(sprintf('%s = %s', $attribute, Translator::getXpathLiteral($value)));
    }

    public function translateIncludes(XPathExpr $xpath, string $attribute, ?string $value): XPathExpr
    {
        return $xpath->addCondition($value ? sprintf(
            '%1$s and contains(concat(\' \', normalize-space(%1$s), \' \'), %2$s)',
            $attribute,
            Translator::getXpathLiteral(' '.$value.' ')
        ) : '0');
    }

    public function translateDashMatch(XPathExpr $xpath, string $attribute, ?string $value): XPathExpr
    {
        return $xpath->addCondition(sprintf(
            '%1$s and (%1$s = %2$s or starts-with(%1$s, %3$s))',
            $attribute,
            Translator::getXpathLiteral($value),
            Translator::getXpathLiteral($value.'-')
        ));
    }

    public function translatePrefixMatch(XPathExpr $xpath, string $attribute, ?string $value): XPathExpr
    {
        return $xpath->addCondition($value ? sprintf(
            '%1$s and starts-with(%1$s, %2$s)',
            $attribute,
            Translator::getXpathLiteral($value)
        ) : '0');
    }

    public function translateSuffixMatch(XPathExpr $xpath, string $attribute, ?string $value): XPathExpr
    {
        return $xpath->addCondition($value ? sprintf(
            '%1$s and substring(%1$s, string-length(%1$s)-%2$s) = %3$s',
            $attribute,
            strlen($value) - 1,
            Translator::getXpathLiteral($value)
        ) : '0');
    }

    public function translateSubstringMatch(XPathExpr $xpath, string $attribute, ?string $value): XPathExpr
    {
        return $xpath->addCondition($value ? sprintf(
            '%1$s and contains(%1$s, %2$s)',
            $attribute,
            Translator::getXpathLiteral($value)
        ) : '0');
    }

    public function translateDifferent(XPathExpr $xpath, string $attribute, ?string $value): XPathExpr
    {
        return $xpath->addCondition(sprintf(
            $value ? 'not(%1$s) or %1$s != %2$s' : '%s != %s',
            $attribute,
            Translator::getXpathLiteral($value)
        ));
    }

    /**
     * {@inheritdoc}
     */
    public function getName()
    {
        return 'attribute-matching';
    }
}
    
asked by anonymous 22.01.2018 / 21:23

1 answer

5

TL; DR

  • The error is not in method translateExists as reported. The problem is that you're using a version of PHP less than the 7.1

About the error

Since version 7.0 of PHP , we can use the methods as follows.

<?php

declare(strict_types=1);

class myClass
{
    public function imprimir(string $msg) : string
    {
        return $msg;
    }
}

$obj = new myClass;
echo $obj->imprimir("Hello World");

This string $name basically serves to force the developer to pass a variable of type string .

If you try to pass a null , for example, you will get the error Fatal error: Uncaught TypeError: Argument 1 passed to myClass::imprimir() must be of the type string, null given .

Demo

But many developers end up having a problem when passing an argument of type null (Many times devs did not validate before).

This has led to the developers of PHP to add the ? operator before the variable type to inform that that method, in addition to receiving the default type, can also receive the value of null . >

In other words, starting from version 7.1 you can use the ? operator and inform the parameter as null that you will not get the error above.

<?php

declare(strict_types=1);

class myClass
{
    public function imprimir(?string $msg) : ?string
    {
        return $msg;
    }
}

$obj = new myClass;
echo $obj->imprimir(null);

Demo

Correction Forms

To fix this you have two options: Remove the ? operator from all files or update the version of your PHP .

I recommend that you use the most recent and stable summer of PHP . In addition to improvements you will not have to be removing code every time you use composer update , for example.

Imagine you have to change this in hundreds of files and every new update of the composer, have to do the same thing.

    
22.01.2018 / 22:37