Open olegabr opened 3 years ago
The following ClassmapReplacer.php
will skip classnames that already begin with the prefix.
<?php
/**
* The purpose of this file is to find and update classnames (and interfaces...) in their declarations.
* Those replaced are recorded and their uses elsewhere are updated in a later step.
*/
namespace CoenJacobs\Mozart\Replace;
class ClassmapReplacer extends BaseReplacer
{
/** @var string[] */
public $replacedClasses = [];
/** @var string */
public $classmap_prefix;
public function replace($contents)
{
return preg_replace_callback(
"
/ # Start the pattern
namespace\s+[a-zA-Z0-9_\x7f-\xff\\\\]+[;{\s\n]{1}.*?(?=namespace|$)
# Look for a preceeding namespace declaration, up until
# a potential second namespace declaration
| # if found, match that much before repeating the search
# on the remainder of the string
[^a-zA-Z0-9_\x7f-\xff]+ # After a non-class character,
{$this->classmap_prefix} # match an already prefixed classname
[a-zA-Z0-9_\x7f-\xff]+ # before continuing on the remainder of the string
|
(?:abstract\sclass|class|interface)\s+ # Look behind for class, abstract class, interface
([a-zA-Z0-9_\x7f-\xff]+) # Match the word until the first
# non-classname-valid character
\s? # Allow a space after
(?:{|extends|implements|\n) # Class declaration can be followed by {, extends,
# implements, or a new line
/sx", // # dot matches newline, ignore whitespace in regex.
function ($matches) use ($contents) {
// If we're inside a namespace other than the global namesspace, just return.
if (preg_match('/^namespace\s+[a-zA-Z0-9_\x7f-\xff\\\\]+[;{\s\n]{1}.*/', $matches[0])) {
return $matches[0];
}
if (preg_match("/[^a-zA-Z0-9_\x7f-\xff]+{$this->classmap_prefix}[a-zA-Z0-9_\x7f-\xff]+/", $matches[0])) {
return $matches[0];
}
// The prepended class name.
$replace = $this->classmap_prefix . $matches[1];
$this->saveReplacedClass($matches[1], $replace);
return str_replace($matches[1], $replace, $matches[0]);
},
$contents
);
}
public function saveReplacedClass($classname, $replacedName)
{
$this->replacedClasses[ $classname ] = $replacedName;
}
}
It is PR #101 with this added:
[^a-zA-Z0-9_\x7f-\xff]+ # After a non-class character,
{$this->classmap_prefix} # match an already prefixed classname
[a-zA-Z0-9_\x7f-\xff]+ # before continuing on the remainder of the string
|
Updating Replacer.php:125
to :
'/(.*)([^a-zA-Z0-9_\x7f-\xff\\\\])'. $original . '([^a-zA-Z0-9_\x7f-\xff])/U',
fixes the extends
incorrect replacement but needs a bit more thought and testing before making a PR.
PhpStorm was highlighting the namespace in Symphony/Polyfill/Intl/Normalizer/Normalizer.php
with "undefined namespace Intl" and "undefined constant Normalizer". I changed composer.json
's "dep_namespace": "\\Ethereumico\\Epg\\Dependencies\\",
to "dep_namespace": "Ethereumico\\Epg\\Dependencies\\",
and the message went away.
That PR that was referred above is now released as part of Mozart 0.6.0. @olegabr can you confirm this is now resolved?
It created file
/home/USER/PROJECT/vendor-epg/classes/symfony/polyfill-intl-normalizer/Resources/stubs/Normalizer.php
with contents:Three issues here:
Symfony\Polyfill\Intl\
namespace is not prefixed withEthereumico\Epg\Dependencies\
ether_and_erc20_tokens_woocommerce_payment_gateway_ether_and_erc20_tokens_woocommerce_payment_gateway_ether_and_erc20_tokens_woocommerce_payment_gateway_ether_and_erc20_tokens_woocommerce_payment_gateway_Normalizer
The
/home/USER/PROJECT/vendor-epg/Symfony/Polyfill/Intl/Normalizer/Normalizer.php
file is defined correctly as: