phpowermove / php-code-generator

PHP code generator library
https://php-code-generator.readthedocs.org
Apache License 2.0
147 stars 43 forks source link

incompatible with php-parser v2.* #38

Closed seancheung closed 7 years ago

seancheung commented 7 years ago

Please add support for php-parser v2.*

Problem 1
    - Installation request for gossi/php-code-generator ^0.3.2 -> satisfiable by gossi/php-code-generator[v0.3.2].
    - Conclusion: remove nikic/php-parser v2.1.1
    - Conclusion: don't install nikic/php-parser v2.1.1
    - gossi/php-code-generator v0.3.2 requires nikic/php-parser ~1 -> satisfiable by nikic/php-parser[v1.0.0, v1.0.1, v1.0.2, v1.1.0, v1.2.0, v1.2.1, v1.2.2, v1.3.0, v1.4.0, v1.4.1].
    - Can only install one of: nikic/php-parser[v1.2.1, v2.1.1].
    - Can only install one of: nikic/php-parser[v1.2.2, v2.1.1].
    - Can only install one of: nikic/php-parser[v1.3.0, v2.1.1].
    - Can only install one of: nikic/php-parser[v1.4.0, v2.1.1].
    - Can only install one of: nikic/php-parser[v1.4.1, v2.1.1].
    - Can only install one of: nikic/php-parser[v1.0.0, v2.1.1].
    - Can only install one of: nikic/php-parser[v1.0.1, v2.1.1].
    - Can only install one of: nikic/php-parser[v1.0.2, v2.1.1].
    - Can only install one of: nikic/php-parser[v1.1.0, v2.1.1].
    - Can only install one of: nikic/php-parser[v1.2.0, v2.1.1].
    - Installation request for nikic/php-parser (locked at v2.1.1) -> satisfiable by nikic/php-parser[v2.1.1].
gossi commented 7 years ago

Unfortunately, because the lib requires sami in require-dev which itself requires the php-parser in version 1. So I'm going with that one so far (I recently tried to update to v2 last week). What would work though is to make "nikic/php-parser": "~1|~2". That would require that the API is consistent between these two major versions. I haven't checked this yet (despite realizing php-parser in v3 is coming soon). Do you know if this works?

seancheung commented 7 years ago

unfortunately it doesn't work in my case. "nikic/php-parser": "^2.1.1" is a dependency of one of my major dependency.

So no chance to make it work with v2.* ?

gossi commented 7 years ago

Well, I can do this:

  "require": {
    ...
    "nikic/php-parser": "~1|~2"
    ...
  }

which would install v1 for dev-mode (as composer install --dev would require v1) and would install v2 for you. That would work in regards to composer. The thing I am wondering about is wether v1 and v2 are API compatible. Like would php-parser v2 break the php-code-generator or not, that is the question. I would be happy to make the lib work with php-parser in both versions 😃

seancheung commented 7 years ago

Man I did a test using composer's local repositories feature.

"require": {
        "gossi/php-code-generator": "*"
    },
"repositories": [
        {
            "type": "path",
            "url": "../php-code-generator"
        }
    ]

Then I changed your package's requirement of php-parser to

"require" : {
    "nikic/php-parser" : "~1|~2"
},

And it successfully installed. image

The sample code runs correctly:

use gossi\codegen\generator\CodeGenerator;
use gossi\codegen\model\PhpClass;
use gossi\codegen\model\PhpMethod;
use gossi\codegen\model\PhpParameter;

$class = new PhpClass();
$class
    ->setQualifiedName('my\\cool\\Tool')
    ->setMethod(PhpMethod::create('__construct')
        ->addParameter(PhpParameter::create('target')
            ->setType('string')
            ->setDescription('Creates my Tool')
        )
    )
;

$generator = new CodeGenerator();
$code = $generator->generate($class);

=>

namespace my\cool;

class Tool {

    /**
     *
     * @param $target string Creates my Tool
     */
    public function __construct($target) {
    }
}

Except that I got a deprecated-error:

PHP error:  The Symfony\Component\OptionsResolver\OptionsResolver::setOptional method is deprecated since version 2.6 and will be removed in 3.0. Use the setDefined() method instead. in F:\Documents\workspace\eyesar-laravel\vendor\symfony\options-resolver\OptionsResolver.php on line 1126
PHP error:  Calling the Symfony\Component\OptionsResolver\OptionsResolver::setAllowedTypes method with an array of options is deprecated since version 2.6 and will be removed in 3.0. Use the new signature with a single option instead. in F:\Documents\workspace\eyesar-laravel\vendor\symfony\options-resolver\OptionsResolver.php on line 591

I think php-parser's v1 is mostly compatible with v2.

gossi commented 7 years ago

I recently pushed to github. I set v1 or v2 for php-parser, so you can play around with dev-master version of this lib. If everything goes well, this will be version 0.4 which I'm about to release in the coming days.

seancheung commented 7 years ago

Sure thanks.

gossi commented 7 years ago

So, the parser instantiation is different between v1 and v2. I tested it today and it failed. So I made some customizations and now the right parser for the right version is being used. I'm about to release 0.4 now, which will work with php-parser v2.