- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 3.7k
Type safe comparison in libraries/cms - second iteration #13276
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
Type safe comparison in libraries/cms - second iteration #13276
Conversation
Type safe comparison in libraries/cms - second iteration - some bool - some int
| { | ||
| // Assume a lone open tag is invalid HTML. | ||
| if ($length == 1 && substr($text, 0, 1) == '<') | ||
| if ($length === 1 && substr($text, 0, 1) === '<') | 
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.
Are we sure lenght is an int?
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.
Thanks to not having the ability to do scalar typehinting, no, we aren't. That said, any dev passing a string into a method declaring a parameter as an integer is just asking for trouble as newer PHP releases come out.
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.
Yes. I agree. Still to keep b/c maybe is better to (int) the var....
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.
It's not a B/C break if the code enforces correct typing of a parameter.  Otherwise either we bump to PHP 7 and typehint everything or remove all typehints and we're stuck running gettype() and is_a() or instanceof checks on everything because the documentation is unreliable.
Sooner or later the mentality of "well just pass anything in any ol' format" has to be broken.  There are far too many issues dealing with something like 0 == '0' == false == null.
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.
OK. If is not considered a b/c break by the project fine. Just raising the question. For the rest obvisualy i agree.
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 totally agree with @mbabker. 👍
|  | ||
| // If the original HTML string is shorter than the $maxLength do nothing and return that. | ||
| if ($baseLength <= $maxLength || $maxLength == 0) | ||
| if ($baseLength <= $maxLength || $maxLength === 0) | 
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.
Are we sure maxlenght is an int?
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.
Leaving as is, because the variable is defined as a parameter to the function, with type integer.
|  | ||
| // Deal with maximum length of 1 where the string starts with a tag. | ||
| if ($maxLength == 1 && substr($html, 0, 1) == '<') | ||
| if ($maxLength === 1 && substr($html, 0, 1) === '<') | 
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.
Same as above
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.
Leaving as is, because the variable is defined as a parameter to the function, with type integer.
| { | ||
| // Do not return itself as result | ||
| if ((int) $item->{$pk} != $id) | ||
| if ((int) $item->{$pk} !== $id) | 
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.
Are we sure id is an int?
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.
Leaving as is, because the variable is defined as a parameter to the function, with type integer.
| $defaults = false; | ||
|  | ||
| if (is_null($plugin) && $autocreate == true && is_null($dispatcher)) | ||
| if (is_null($plugin) && $autocreate === true && is_null($dispatcher)) | 
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.
Are we sure autocreate is a boolean?
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.
Leaving as is, because the variable is defined as a parameter to the function, with type boolean.
        
          
                libraries/cms/table/corecontent.php
              
                Outdated
          
        
      |  | ||
| // If checkin is supported and all rows were adjusted, check them in. | ||
| if ($checkin && (count($pks) == $this->_db->getAffectedRows())) | ||
| if ($checkin && (count($pks) === $this->_db->getAffectedRows())) | 
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.
Are we sure affected rows return an int in all db drivers?
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.
That's a good question. But normally int is supported in every common db and so should the drivers. Will check, though, to be sure. Or we just cast to int, to be on the safe side
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.
@andrepereiradasilva Yes, we are sure now. I just checked. :)
On, to review & merge?
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.
according to comment above
# Conflicts: # libraries/cms/component/router/rules/standard.php # libraries/cms/html/string.php
| Conflicts resolved. Pls check and merge. | 
…nto tye-safety-in-libraries-cms-2
| @andrepereiradasilva & @Quy Would you be so nice to code review this, too. So we can RTC this and after merging, tackle the parent PR (when all its Sub PRs are merged)? Again, thank you for your efforts!! | 
        
          
                libraries/cms/html/html.php
              
                Outdated
          
        
      |  | ||
| $prefix = (count($parts) == 3 ? array_shift($parts) : 'JHtml'); | ||
| $file = (count($parts) == 2 ? array_shift($parts) : ''); | ||
| $prefix = (count($parts) === 3 ? array_shift($parts) : 'JHtml'); | 
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.
Remove parentheses (both lines).
        
          
                libraries/cms/table/corecontent.php
              
                Outdated
          
        
      |  | ||
| // If checkin is supported and all rows were adjusted, check them in. | ||
| if ($checkin && (count($pks) == $this->_db->getAffectedRows())) | ||
| if ($checkin && (count($pks) === $this->_db->getAffectedRows())) | 
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.
Remove parentheses
| I have tested this item ✅ successfully on f5fe9bf This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/13276. | 
| I have tested this item ✅ successfully on f5fe9bf This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/13276. | 
| RTC after two successful tests. | 
Summary of Changes
Type safe comparison in libraries/cms - second iteration
This PR is part of a set to try to separate some of the changes done in one of my previous batch PR's for the libraries/cms directory, which is still on hold (#12171).
Once the new set is merged completely, it will hopefully reduce the changes in that PR, so it can be reviewed easier and finally be merged.
The changes in this PR should be also be fairly easy to review. In hope that this will get merged quickly. ;)
Note: Don't bother if some possible changes are missing or could be differently written. They are probably in the batch PR , that this one references. As soon as this set of sub PR's is merged, the batch PR will have its conflicts resolved and should be a lot easier to review and finally get merged.
Testing Instructions
None, should not change behavior
Documentation Changes Required
None.