Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle Carbon macros #301

Merged
merged 9 commits into from
Aug 28, 2019
Merged

Conversation

kylekatarnls
Copy link
Contributor

Hi,

I would like to propose a compatibility support for Carbon macros, it uses the same public API as Laravel macros but is different internally.

I tested the code above in Laravel apps and it works fine.

But I couldn't get a correct unit tests I mimicked other ones but if I revert my change in source, the CarbonExtension test still pass while I should get "undefined method" PHPStan errors.

If you're OK to include this feature, could you help me to write this unit test?

@szepeviktor
Copy link
Collaborator

Very well done.

Could you make the test pass?

@kylekatarnls
Copy link
Contributor Author

@szepeviktor See my comment. On my local machine the tests are all green rather I tests with or without my change, and in Travis-CI it's like ignored. But in real apps, it's working. I can't guess more how the feature test is supposed to work.

@kylekatarnls
Copy link
Contributor Author

I guess tests are not compatible with Windows as running php artisan code:analyse inside the directory "laravel" created by laravel-test.sh output the content of ./vendor/bin/phpstan instead of running it. However I can run php artisan code:analyse with no troubles in real apps.

@kylekatarnls
Copy link
Contributor Author

I found what was not compatible with Windows:

  • starts_with($path, DIRECTORY_SEPARATOR) is Unix root only, this means --paths currently does not work with absolute paths on Windows, I fixed it.
  • tests compare paths assuming directory separator is "/"
  • tests analyze() method calls the shell/bat in vendor/bin, it should call the PHP bin in vendor/phpstan/phpstan/bin which is universal

Now I get the tests working on my local machine, I will check the change has not the same effect in the test app than in my local app.

@kylekatarnls
Copy link
Contributor Author

Now, it needs a way to declare PhpMethodReflection as "static", when a macro is explicitly static like in the test example:

Carbon::macro('foo', static function (): string {
    return 'foo';
});

Such macros should be callable both statically or dynamically. And not only in Carbon but also in the Laravel Macroable stuff.

@kylekatarnls
Copy link
Contributor Author

I see $this->isStatic = false; inside the Macro which is incorrect. They can be both. And we can consider a best practice to use static function to declare them as static, but even without the static statement, macros are callable statically, so they should more likely be considered static by default.

@ondrejmirtes
Copy link
Contributor

ondrejmirtes commented Aug 21, 2019 via email

@kylekatarnls
Copy link
Contributor Author

kylekatarnls commented Aug 21, 2019

It should be marked as static and ignore “dynamic call to static method” in
phpstan.neon

It would also apply to non-macros methods so I'm not a fan of this fix. I would rather say macros are static. At least they should be when the explicit statement is added.

I think it already is, as calling dynamically the static method do not raise errors.

I proposed here a softer change that will only mark static macros if they are created with static closures. If behavior is changed for all macros it can be an other PR/discussion.

@kylekatarnls kylekatarnls marked this pull request as ready for review August 21, 2019 11:27
@kylekatarnls
Copy link
Contributor Author

@szepeviktor seems like there is a broken hook:
continuous-integration/styleci/push Expected — Waiting for status to be reported

But as I just updated the branch with master it means it's the same exact code than for the continuous-integration/styleci/pr check.

@@ -24,7 +24,7 @@
},
"require-dev": {
"orchestra/testbench": "^3.8",
"phpunit/phpunit": "^8.2"
"phpunit/phpunit": "^7.3 || ^8.2"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a fix for 443f0fe
In order to have successful unit tests.

@nunomaduro nunomaduro merged commit 6307a01 into larastan:master Aug 28, 2019
@kylekatarnls kylekatarnls deleted the feature/carbon-macros branch August 28, 2019 08:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants