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

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

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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)