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

Classmap implementation #4

Merged
merged 15 commits into from
Mar 24, 2017
Merged

Classmap implementation #4

merged 15 commits into from
Mar 24, 2017

Conversation

coenjacobs
Copy link
Owner

My first stab at getting #2 ready. A little rough around the edges, but ready for a second pair of eyes if anyone has a couple minutes.

return preg_replace_callback(
'/(?:\W[abstract]*class |interface )([a-zA-Z\_]+)[ ]*{/U',
function ($matches) {
$replace = 'CJDT_' . $matches[1];
Copy link
Owner Author

Choose a reason for hiding this comment

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

The CJDT_ prefix is hardcoded here, needs to be replaced by a config variable.

Copy link
Owner Author

Choose a reason for hiding this comment

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

Fixed: a35ee8c

class Classmap implements Autoloader
{
/** @var array */
public $paths = [];
Copy link
Owner Author

Choose a reason for hiding this comment

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

Classmap autoloader should support having files in the array, not just directories. Potentially look into splitting the $paths variable up in $directories and $files.

src/Mover.php Outdated
$targetFile = str_replace($this->workingDir, $this->config->dep_directory, $file->getRealPath());
} else {
$namespacePath = str_replace('\\', '/', $autoloader->namespace);
if (is_a($autoloader, NamespaceAutoloader::class)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

You should prefer instanceof operator instead of is_a() function. The operator doesn't incur a function overhead, and is_a() was only de-deprecated because of popular request. It should have been gone already.

Copy link
Owner Author

Choose a reason for hiding this comment

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

Thanks, fixed: 05fea7d

src/Mover.php Outdated

foreach( $this->replacedClasses as $original => $replacement ) {
$contents = preg_replace_callback(
'/\W('.$original.')(?:\(|\:\:)/U',
Copy link
Contributor

Choose a reason for hiding this comment

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

This will miss a couple of scenarios.

The ones I can think of right now:

  1. type-hints: public function __construct( MyClass $obj );
  2. new without arguments: $obj = new MyClass;
  3. class in string: $class = 'MyClass'; $obj = new $class();

It will also probably break when using relative namespaces, like the following:

namespace Vendor;
throw new Exception\MyException(); // FQCN: \Vendor\Exception\MyException

I'm not really sure how you can add cases like these without getting false positives as well, though. I don't think this can properly be handled by regexes, you should look into using nikic/PHP-Parser perhaps.

Copy link
Owner Author

Choose a reason for hiding this comment

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

Thanks @schlessera, I would love to make this as bullet proof as I can, but don't want this to distract too much from actually implementing the classmap handler. Improving the replacing logic can obviously be an ongoing process. Can you please post this as an issue, so I can handle it accordingly ( - it would be amazing if the GitHub UI allowed me to promote this comment to an issue, directly from here)?

@coenjacobs coenjacobs merged commit 46d3046 into master Mar 24, 2017
@coenjacobs coenjacobs deleted the classmap-implementation branch March 24, 2017 10:06
szepeviktor pushed a commit to szepeviktor/BrianHenryIE_mozart that referenced this pull request May 8, 2021
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.

2 participants