-
Notifications
You must be signed in to change notification settings - Fork 105
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
Only prepend namespace for known classes #702
Only prepend namespace for known classes #702
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good addition, but tests now fail. Please take a look.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if i remember correctly class and namespace checks (is_a, instanceof, class_exists) are all checked by advancing char position.
For performance is better not to recheck an already checked class?
i mean if the requested identifier was already requested answer directly with previously populated value of array.
Is a good feature but i suggest add a @todo can be improved caching results in array?
nevermind, profiling the case it shows that is still checked by char like the other comparison class/interface functions, but cache is not needed. |
src/App.php
Outdated
@@ -376,7 +376,10 @@ public function addStyle($style) | |||
*/ | |||
public function normalizeClassNameApp($name) | |||
{ | |||
return '\\'.__NAMESPACE__.'\\'.$name; | |||
if (class_exists('\\'.__NAMESPACE__.'\\'.$name)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NOTE: I think we need to talk about declaration of argument type :
- in comments for static analysis - fast no control at runtime
- in function declaration raise exception - "slow" strict check at runtime
related video of Rasmus Lerdorf - https://www.youtube.com/watch?v=rKXFgWP-2xQ
php 5.6 support ended on 01-01-2019 ( https://www.php.net/supported-versions.php )
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/* can be defined as constant */
$ns_sepa = chr(92);
/** test against untouched name */
if (class_exists($name)) {
return $name;
}
/** normalize namespace */
$normalized_namespace = $ns_sepa.__NAMESPACE__;
/** normalize name */
$normalized_name = $ns_sepa.trim($name,$ns_sepa);
/** test against normalize name */
if (class_exists($normalized_name)) {
return $normalized_name;
}
/** test against normalized namespace + normalize name */
if (class_exists($normalized_namespace.$normalized_name)) {
return $normalized_namespace.$normalized_name;
}
/** last chance to check it */
/* give error even with this extra check
$atk4_namespace = $ns_sepa.'atk4/ui';
if (class_exists($atk4_namespace.$normalized_name)) {
return $atk4_namespace.$normalized_name;
}
*/
/* it will throw exception for sure check to help sleepy dev */
// char(47) = / aka wrong slash used as separator
if(strpos($name,chr(47)) !== false)
{
throw new Exception('Wrong separator used for class, detect / in place of \ called from class ' . $name);
}
/** class not exists DIE */
throw new Exception('Class with name ' . $name . ' not exists, checked ' .
$normalized_name. ' | ' . $normalized_namespace . $normalized_name);
BUT even if in this way it works as intended, it redeclare classes and throw errors in some demos unittest.
Errors are due to misuse of wrong slash in place of backslash in $name.
We can make a str_replace and lose performance and solving a problem of the dev, in my opinion this is wrong, or throw an exception and make it changes the code or add a flag and make this function switchable in App.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i remember somewhere there was something on this subject of usage of slash in place of backslash
https://agile-core.readthedocs.io/en/latest/factory.html#namespace
we need to find where to put the str_replace of slash to backslash, before all or at the end and make another call to the function and finally throw exception?
My suggested version, it pass all tests :
public function normalizeClassNameApp($name)
{
/* can be defined as constant */
$ns_sepa = chr(92);
/** test against untouched name */
if (class_exists($name)) {
return $name;
}
/** normalize namespace */
$normalized_namespace = $ns_sepa.__NAMESPACE__;
/** normalize name */
$normalized_name = $ns_sepa.trim($name,$ns_sepa);
/** test against normalize name */
if (class_exists($normalized_name)) {
return $normalized_name;
}
/** test against normalized namespace + normalize name */
if (class_exists($normalized_namespace.$normalized_name)) {
return $normalized_namespace.$normalized_name;
}
/** last chance to check it */
/* give error even with this extra check
$atk4_namespace = $ns_sepa.'atk4/ui';
if (class_exists($atk4_namespace.$normalized_name)) {
return $atk4_namespace.$normalized_name;
}
*/
/* it will throw exception for sure check to help sleepy dev */
// char(47) = / aka wrong slash used as separator
if(strpos($name,chr(47)) !== false)
{
return $this->normalizeClassNameApp(str_replace(chr(47), chr(92), $name));
}
/** class not exists DIE */
throw new Exception('Class with name ' . $name . ' not exists, checked ' . $normalized_name. ' | ' . $normalized_namespace . $normalized_name);
}
Why not using it like
|
IMHO because :
i'm working on \atk4\core\ConfigTrait to be used like slim with callback in config to instantiate objects against interface implementation like :
|
OK lets discuss on a hangout later. |
will keep using "class_exist" here even if it has some performance hit. |
this can solve every case
Codecov Report
@@ Coverage Diff @@
## develop #702 +/- ##
============================================
- Coverage 72.55% 69.8% -2.76%
- Complexity 1933 2356 +423
============================================
Files 105 106 +1
Lines 4559 5348 +789
============================================
+ Hits 3308 3733 +425
- Misses 1251 1615 +364
Continue to review full report at Codecov.
|
1 similar comment
Codecov Report
@@ Coverage Diff @@
## develop #702 +/- ##
============================================
- Coverage 72.55% 69.8% -2.76%
- Complexity 1933 2356 +423
============================================
Files 105 106 +1
Lines 4559 5348 +789
============================================
+ Hits 3308 3733 +425
- Misses 1251 1615 +364
Continue to review full report at Codecov.
|
@romaninsh OK, i check the flow and i see that exception for class existence is throw in atk4/core/FactoryTrait |
Looks like a better implementation is in #726. Perhaps I can split that out into a separate PR. |
Oh you've done that for me!! yey. |
Currently
atk4\ui\App
normalize class will prepend\atk4\ui\
when using short name of the class, like this:However, this requires us to always use full name for the class:
This is incompatible with:
because it uses
my\test\MyClass
without opening backslash.This PR fixes problem by checking for the presence of the class inside \atk4\ui\ before attempting to use the prefix.