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

Determine tmpDir path relative to config file when appropriate #163

Merged
merged 11 commits into from
Dec 14, 2021

Conversation

mattwellss
Copy link
Contributor

Fix #161

@shochdoerfer shochdoerfer added this to the 0.12.0 milestone Nov 30, 2021
@shochdoerfer shochdoerfer self-assigned this Nov 30, 2021
@shochdoerfer
Copy link
Member

Very much appreciated. Thanks a lot for improving this PHPStan extension!

@@ -46,7 +46,11 @@
if (!empty($configFile)) {
$neonConfig = Neon::decode(file_get_contents($configFile));
if(is_array($neonConfig) && isset($neonConfig['parameters']) && isset($neonConfig['parameters']['tmpDir'])) {
$tmpDir = $neonConfig['parameters']['tmpDir'];
Copy link
Member

Choose a reason for hiding this comment

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

We should add a call to \Nette\DI\Helpers::expand() in the same way as PHPstan does it to make sure we are dealing with the same configuration: https://github.com/phpstan/phpstan-src/blob/master/src/Command/CommandHelper.php#L149

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 think in that case, we'll need to add nette/di as a dependency (probably not a big deal). The class Nette/DI/Helpers is marked @internal, which my IDE treats like sorta like a deprecated class.

Given those points, are you comfortable using that method? I'd like to follow your lead on decisions like this.

autoload.php Outdated
// otherwise, treat the tmpDir as a path relative to the directory of the config file
$tmpDir = strpos($neonConfig['parameters']['tmpDir'], '/') === 0
? $neonConfig['parameters']['tmpDir']
: dirname($configFile) . '/' . $neonConfig['parameters']['tmpDir'];
Copy link
Member

Choose a reason for hiding this comment

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

I feel we might not need this bock and rather add the same !isset() check as PHPStan uses: https://github.com/phpstan/phpstan-src/blob/master/src/Command/CommandHelper.php#L228-L231

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is it this check that we should replace with the simpler isset: https://github.com/bitExpert/phpstan-magento/pull/163/files/137b8dfd770612cc9cc057d6aaf537cfb650dffc#diff-f79e1f84a3df304b5be8b1bd2f1a36d4bb8417d91e8ae7144e648379d697a57eL48?

If not, I think I need some clarification regarding the change you'd like to see.

@shochdoerfer
Copy link
Member

@mattwellss sorry for not getting back to you earlier. I am still super busy with my work, this should hopefully get better next week. I had the chance to discuss things with Ondřej. He pointed out that the bootstrap files get a $container instance injected from which we can pull the dependencies instead of creating them on our own. Additionally, that means, we can omit the whole config parsing logic.

See here how the bootstrap files are invoked: https://github.com/phpstan/phpstan-src/blob/master/src/Command/CommandHelper.php#L475

If you find some time to refactor the logic, feel free. Otherwise, I'll do it next week or the week after. My goal is to try and get the next extension release done before Christmas with both of your PR's included.

autoload.php Outdated Show resolved Hide resolved
@mattwellss
Copy link
Contributor Author

@shochdoerfer wow, the autoloader is so much cleaner using the $container! What an improvement, thanks for finding that.

Does this change mean we can eliminate the nette/neon dependency from composer.json?

@shochdoerfer
Copy link
Member

@mattwellss agree, looks so much better. Yes, we don't need the nette/neon dependency anymore. Good catch.

Also, we can improve things a bit more ;) We can define the autoloader classes as services in the extension.neon file and then pull them from the DI container.

As it looks to me, we can declare the services in the extensions.neon file this:

mockAutoloader :
		class: bitExpert\PHPStan\Magento\Autoload\MockAutoloader

and then pull it from the container like this:

$mockAutoloader= $container->getService('mockAutoloader');

Since the require_once() in PHPStand is embedded in a closure already, we could also get rid of this I think:

(function (Container $container) {
})($container);

- define autoloaders as an "extend-able" configuration
- ensure autoloaders needing custom cache storage have it
- warn when a container or autoloader service isn't found
@mattwellss
Copy link
Contributor Author

OK, I got a little wild with the latest commit, happy to roll back some of my decisions if needed. 😄

The goal with my latest changes is to allow users of the extension to define their own autoloaders as needed. This results in a lot of extra configuration in extension.neon, and might be totally unnecessary, if this extension is intended to cover all potential autoloading needs.

Curious to hear your thoughts.

@shochdoerfer
Copy link
Member

I like your way of thinking but we should adjust things a little bit ;)

  1. I feel it would be better to introduce an interface for the autoloader classes and omit the Magento autoloaders config section in extension.neon. The interface could be defined as:
interface Autoloader
{
    public function autoload(string $class): void;

    public function canThrow(): bool;
    
    public function canPrepend(): bool;
}
  1. In extension.neon add a tag for the autoloader services like phpstan.magento.autoloader. Then in autoload.php you could query all tagged autoloader services via $container->getServicesByTag('phpstan.magento.autoloader').

  2. Since we have the interface as defined above, you can get the autoloader configuration by querying the respective methods:

\spl_autoload_register([$autoloader, $method], $autoloader->canThrow(), $autoloader->canPrepend());

What do you think?

Is the FileCacheStorage directory correctly configured? You set it to %tmpDir%, in the PHPStan config it is set to %tmpDir%/cache/PHPStan (and I think I used that path in the old autoload.php implementation). Or am I missing something?
https://github.com/phpstan/phpstan-src/blob/master/conf/config.neon#L1560

@mattwellss
Copy link
Contributor Author

Good suggestions re: defining autoloaders! I'll work on that.

Oops, you're totally right about the $directory argument for cache storage... I'll update it. Do you know if we can extract the value configured by PHPStan's core?

@shochdoerfer
Copy link
Member

I don't think we can pull the value from the DI container, because it's defined inline. Leave it as it is, for now, that's fine for me. I will try to figure out what was the reason for creating the FileCacheStorage class. Maybe we can drop that and depend on PHPStan's storage class. Then we could avoid the problem of having 2 cache directory definitions.

Maybe we should add one minor thing to the autoload.php file: And instanceof check in the foreach loop would be nice. Just to avoid that someone adds the tag to a different class.

@mattwellss
Copy link
Contributor Author

I will try to figure out what was the reason for creating the FileCacheStorage class.

PHPStan's built-in FileCacheStorage returns the contents of the cached item, but we want the path of the cached item. I don't imagine we want to start using eval, but I suppose that approach would allow us to use the built-in cache.

I'll add a type check for the autoloader instance

@shochdoerfer
Copy link
Member

@mattwellss the final PHPStan complaint needs to be fixed like this: https://github.com/bitExpert/phpstan-magento/blob/master/phpstan.neon#L11

Thanks for doing the annoying work ;)

@mattwellss
Copy link
Contributor Author

Neat! I don't mind adding types and such. That's just PHPStan forcing us to be careful

@shochdoerfer shochdoerfer merged commit 05b71a3 into bitExpert:master Dec 14, 2021
@shochdoerfer
Copy link
Member

And merged ;)

@mattwellss
Copy link
Contributor Author

@shochdoerfer I forgot to remove nette/neon 😖 sorry!

@mattwellss mattwellss deleted the bugfix/161 branch December 15, 2021 15:16
@shochdoerfer
Copy link
Member

No worries, I have it on my list to fix ;)

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.

tmpDir should be relative to PHPStan configuration file path
2 participants