zephir-lang / zephir

Zephir is a compiled high-level language aimed to ease the creation of C-extensions for PHP
https://zephir-lang.com
MIT License
3.3k stars 466 forks source link

using const in a class in a non-consecutive order throws a ParseException #1582

Closed chilimatic closed 7 years ago

chilimatic commented 7 years ago

the parser throws an syntax error if const are not declared in an consecutive block

namespace Bugreport;

class ConstOrder {
    const MY_FIRST_CONST = "test";

    private $oneProperty = 1;

    const MY_SECOND_CONST = "test2";
}

zephir\Parser\ParseException: Syntax error in /home/j/development/repos/bugreport-zephir/bugreport/bugreport/constorder.zep on line 7

this works.

namespace Bugreport;
class ConstOrder {
    const MY_FIRST_CONST = "test";
    const MY_SECOND_CONST = "test2";

    private $oneProperty = 1;
}

I know splitting consts with other parameters is ugly and I just ran into this error by accident. At least if we're following the classic patterns and structures.

namespace Bugreport;

class ExampleAction {

    const MY_ACTION_NAME = "MY_ACTION_NAME";
    public static createMyAction(array payload): void {
        return [
            "type": self::MY_ACTION_NAME,
            "payload": payload
        ]
    }

    const MY_OTHER_ACTION_NAME = "MY_OTHER_ACTION_NAME";
    public static createMyOtherAction(array payload): void {
       return [
           "type": self::MY_OTHER_ACTION_NAME,
           "payload": payload
       ]
    }
}

(this is an 1:1 adaption of the declarative CQRS, Redux (js) implementation of an action)

this for example is another way to structure code more contextualized. I know we could just extract this to a flyweight pattern but that's not the point.

The point is, it's an undefined behaviour and just getting "syntax error" is not enough feedback, since it's syntactically correct at least you don't write any specifics in your documentation https://docs.zephir-lang.com/en/latest/oop.html

sergeyklay commented 7 years ago

This issue was moved to phalcon/php-zephir-parser#26