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

Test Carbon mixin #1857

Draft
wants to merge 7 commits into
base: 2.x
Choose a base branch
from
Draft

Test Carbon mixin #1857

wants to merge 7 commits into from

Conversation

spawnia
Copy link
Contributor

@spawnia spawnia commented Feb 15, 2024

  • Added or updated tests
  • Documented user facing changes

Changes

I am trying to reproduce an issue in a project of mine that uses Carbon mixins.

After updating from larastan v2.8.1 to v2.9.0 errors such as the following started appearing:

Update: the issue also exists with v2.8.1, sorry. I am digging further to find what caused the issue.

Second update: I tried updating the dependencies one by one. It was indeed the update from Larastan that was responsible.

bfranke@MLLNB61:~/projects/nemo-api (update-deps)*$ make stan
$( [ -f /.dockerenv ] && echo "" || echo "docker-compose exec nemo-api-php") vendor/bin/phpstan analyse --memory-limit=2048M
Note: Using configuration file /var/www/phpstan.neon.
 836/836 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%

 [OK] No errors                                                                                                         

bfranke@MLLNB61:~/projects/nemo-api (update-deps)*$ make shell
www-data@482021448d96:/var/www$ composer update larastan/larastan
Loading composer repositories with package information
Updating dependencies
Lock file operations: 0 installs, 1 update, 0 removals
  - Upgrading larastan/larastan (v2.8.1 => v2.9.0)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 0 installs, 1 update, 0 removals
  - Upgrading larastan/larastan (v2.8.1 => v2.9.0): Extracting archive
Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover --ansi

   INFO  Discovering packages.

  barryvdh/laravel-ide-helper ................................................................................................................. DONE
  bensampo/laravel-enum ....................................................................................................................... DONE
  erjanmx/laravel-migrate-check ............................................................................................................... DONE
  intervention/image .......................................................................................................................... DONE
  itsgoingd/clockwork ......................................................................................................................... DONE
  laravel/tinker .............................................................................................................................. DONE
  mll-lab/laravel-comment ..................................................................................................................... DONE
  mll-lab/laravel-graphiql .................................................................................................................... DONE
  mll-lab/laravel-graphql-voyager ............................................................................................................. DONE
  mll-lab/laravel-utils ....................................................................................................................... DONE
  nesbot/carbon ............................................................................................................................... DONE
  nunomaduro/collision ........................................................................................................................ DONE
  nunomaduro/laravel-mojito ................................................................................................................... DONE
  nunomaduro/termwind ......................................................................................................................... DONE
  nuwave/lighthouse ........................................................................................................................... DONE
  sentry/sentry-laravel ....................................................................................................................... DONE
  spatie/laravel-activitylog .................................................................................................................. DONE
  spatie/laravel-medialibrary ................................................................................................................. DONE
  staudenmeir/eloquent-has-many-deep .......................................................................................................... DONE
  thedoctor0/laravel-factory-generator ........................................................................................................ DONE

131 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
phpstan/extension-installer: Extensions installed
> bensampo/laravel-enum: installed
> composer/composer: installed
> ekino/phpstan-banned-code: installed
> larastan/larastan: installed
> mll-lab/php-utils: installed
> nesbot/carbon: installed
> phpstan/phpstan-deprecation-rules: installed
> phpstan/phpstan-mockery: installed
> phpstan/phpstan-phpunit: installed
> thecodingmachine/phpstan-safe-rule: installed
> Illuminate\Foundation\ComposerScripts::postUpdate
> @php artisan ide-helper:generate
A new helper file was written to _ide_helper.php
> @php artisan ide-helper:meta
A new meta file was written to .phpstorm.meta.php
> @php artisan lighthouse:ide-helper
Wrote schema directive definitions to /var/www/schema-directives.graphql.
Wrote definitions for programmatically registered types to /var/www/programmatic-types.graphql.
Wrote PHP definitions to /var/www/_lighthouse_ide_helper.php.

It is recommended to add them to your .gitignore file.
> @php artisan vendor:publish --tag=strict-stubs --force

bfranke@MLLNB61:~/projects/nemo-api (update-deps)*$ make stan
$( [ -f /.dockerenv ] && echo "" || echo "docker-compose exec nemo-api-php") vendor/bin/phpstan analyse --memory-limit=2048M
Note: Using configuration file /var/www/phpstan.neon.
 836/836 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%

 ------ ------------------------------------------------------------------------------- 
  Line   app/Models/File.php
 ------ -------------------------------------------------------------------------------
  55     Static call to instance method Carbon\Carbon::valid().                         
         ✏️  app/Models/File.php:55
  63     Static call to instance method Carbon\Carbon::createFromHyphenlessDateTime().
         ✏️  app/Models/File.php:63
  73     Static call to instance method Carbon\Carbon::createFromHyphenlessDateTime().
         ✏️  app/Models/File.php:73
 ------ -------------------------------------------------------------------------------

 ------ --------------------------------------------------------
  Line   app/Models/HistoryModel.php
 ------ --------------------------------------------------------
  112    Static call to instance method Carbon\Carbon::valid().
         ✏️  app/Models/HistoryModel.php:112
  278    Static call to instance method Carbon\Carbon::valid().
         ✏️  app/Models/HistoryModel.php:278
 ------ --------------------------------------------------------

 ------ -------------------------------------------------------------------------------
  Line   app/Models/MllGene.php
 ------ -------------------------------------------------------------------------------
  156    Static call to instance method Carbon\Carbon::createFromHyphenlessDateTime().
         ✏️  app/Models/MllGene.php:156
  166    Static call to instance method Carbon\Carbon::createFromHyphenlessDateTime().
         ✏️  app/Models/MllGene.php:166
 ------ -------------------------------------------------------------------------------

 ------ --------------------------------------------------------
  Line   database/factories/MllGeneFactory.php
 ------ --------------------------------------------------------
  68     Static call to instance method Carbon\Carbon::valid().
         ✏️  database/factories/MllGeneFactory.php:68
 ------ --------------------------------------------------------


 [ERROR] Found 8 errors

  Line   database/factories/MllGeneFactory.php
 ------ --------------------------------------------------------
  68     Static call to instance method Carbon\Carbon::valid().

The code under question:

use Carbon\Carbon;
...
            'valid_to' => Carbon::valid(),

The relevant part of the mixin:

use Carbon\Carbon;

/** @mixin Carbon */
final class CarbonMixin
{
    public static function valid(): \Closure
    {
        return static function (): Carbon {
            // @phpstan-ignore-next-line This never returns false, we use static values
            return Carbon::create(9999, 12, 31, 23, 55, 55);
        };
    }
}

It is registered in a service provider like this:

    public function boot(): void
    {
        Carbon::mixin(new CarbonMixin());
    }

I can confirm that the mixin is recognized, as changing the method name to something nonsensical triggers the following error:

  Line   database/factories/MllGeneFactory.php
 ------ -----------------------------------------------------------
  68     Call to an undefined static method Carbon\Carbon::asdf().

Unfortunately, I am unable to reproduce the issue in a test case here. Can you help?

Breaking changes

Not yet.

@canvural
Copy link
Collaborator

Are you also using PHPStan strict rules?

@spawnia
Copy link
Contributor Author

spawnia commented Feb 15, 2024

Are you also using PHPStan strict rules?

No. I am using https://github.com/phpstan/extension-installer, so I double checked that the strict rules package is not present in vendor.

@calebdw
Copy link
Contributor

calebdw commented Feb 15, 2024

If you're using the extension-installer then the Carbon extension should be automatically installed. Try testing the Carbon mixin without Larastan, and if the issue still persists then it's something on their end.

@@ -1,6 +1,8 @@
includes:
- ../extension.neon
- ../vendor/nesbot/carbon/extension.neon
Copy link
Contributor

Choose a reason for hiding this comment

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

What happens if you remove this line?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The tests I added in Larastan pass with or without the setting.

Copy link
Contributor

Choose a reason for hiding this comment

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

Regardless, we shouldn't be testing their extension in our tests---personally I'm of the mindset that Larastan should remove support for the Carbon mixins as Carbon provides their own extension for their own mixins

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added a comment as to why I think it is useful for testing: d606311

Copy link
Contributor

Choose a reason for hiding this comment

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

I still don't think this is necessary/required---our job is to test our own extension, not what other users might install in their application.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I would argue that Larastan does not exist in isolation, thus a more integration-like test setup is useful.

Copy link
Contributor

Choose a reason for hiding this comment

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

I could see that argument when running the e2e tests, but not when testing the code specific to this package

@szepeviktor
Copy link
Collaborator

Could you fix another typo? ./tests/Type/data/model-properties.php:80 "overriden"

@spawnia
Copy link
Contributor Author

spawnia commented Feb 15, 2024

If you're using the extension-installer then the Carbon extension should be automatically installed. Try testing the Carbon mixin without Larastan, and if the issue still persists then it's something on their end.

Thanks for the feedback. I tried adding the following to my composer.json:

    "extra": {
        "phpstan/extension-installer": {
            "ignore": [
                "nesbot/carbon"
            ]
        }
    }

The resulting errors are exactly the same as before. As I have outlined in the second update to the description, the update from larastan v2.8.1 to v2.9.0 is indeed the single change that causes the issue to appear. Not saying larastan is wrong or buggy, but that triggers the error.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Temporarily removed to allow CI to pass and run on all versions.

@spawnia
Copy link
Contributor Author

spawnia commented Feb 15, 2024

Are you also using PHPStan strict rules?

Here are the included extensions

phpstan/extension-installer: Extensions installed
> bensampo/laravel-enum: installed
> composer/composer: installed
> ekino/phpstan-banned-code: installed
> larastan/larastan: installed
> mll-lab/php-utils: installed
> nesbot/carbon: installed
> phpstan/phpstan-deprecation-rules: installed
> phpstan/phpstan-mockery: installed
> phpstan/phpstan-phpunit: installed
> thecodingmachine/phpstan-safe-rule: installed

@calebdw
Copy link
Contributor

calebdw commented Feb 15, 2024

The resulting errors are exactly the same as before. As I have outlined in the second update to the description, the update from larastan v2.8.1 to v2.9.0 is indeed the single change that causes the issue to appear.

Which version of Carbon are you on?

Also, you should try analyzing the Carbon mixin without Larastan using only the Carbon extension

# Conflicts:
#	tests/Type/GeneralTypeTest.php
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.

None yet

4 participants