duanmu2941
2016-12-14 15:25
浏览 57
已采纳

数组中的尾随逗号导致解析错误

I'm currently working on some project on Symfony 2.8. I've come across this weird PHP error.

I'm building a symfony command that allows using Kahlan test framework inside a Symfony project. Thus, I need the Symfony container to be available in Kahlan test suites, and so to be defined as global variable of said suites. Simple matter with the Container aware command.

But here comes the glitch. When I try to call a service that uses Symfony's security.authorization_checker service, the cli returns this error:

PHP Parse error: syntax error, unexpected ',' in /tmp/kahlan/data/www/project/vendor/symfony/symfony/src/Symfony/Component/Security/Core/Authorization/ExpressionLanguageProvider.php on line 55

The code of said file:

[...]
public function getFunctions()
{
    return array(
        new ExpressionFunction('is_anonymous', function () {
            return '$trust_resolver->isAnonymous($token)';
        }, function (array $variables) {
            return $variables['trust_resolver']->isAnonymous($variables['token']);
        }),

        new ExpressionFunction('is_authenticated', function () {
            return '$token && !$trust_resolver->isAnonymous($token)';
        }, function (array $variables) {
            return $variables['token'] && !$variables['trust_resolver']->isAnonymous($variables['token']);
        }),

        new ExpressionFunction('is_fully_authenticated', function () {
            return '$trust_resolver->isFullFledged($token)';
        }, function (array $variables) {
            return $variables['trust_resolver']->isFullFledged($variables['token']);
        }),

        new ExpressionFunction('is_remember_me', function () {
            return '$trust_resolver->isRememberMe($token)';
        }, function (array $variables) {
            return $variables['trust_resolver']->isRememberMe($variables['token']);
        }),

        new ExpressionFunction('has_role', function ($role) {
            return sprintf('in_array(%s, $roles)', $role);
        }, function (array $variables, $role) {
            return in_array($role, $variables['roles']);
        }),
    );
}
[...]

Line 55 is the line of the last array element, thus said comma is the trailing comma. The error is thrown only in cli. An of course, I have tried removing the comma, everything works then perfectly. But obviously, I can't change Symfony's built-in classes.

So, How come PHP throws Parse Errors for a trailing comma in an array only in cli mode, and do you guys have any idea on how to fix this?

Thanks.

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doutui6644 2016-12-16 09:08
    已采纳

    As seen in the kahlan-bundle repo, The problem was not in my code but seemed to be with kahlan.

    Issue answered by Kahlan dev here.

    Upgrading to the new kahlan version simply resolved the problem.

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题