Skip to content

Conversation

@frankmayer
Copy link
Contributor

@frankmayer frankmayer commented Dec 19, 2016

Summary of Changes

Type safe comparison in libraries/cms - second iteration

  • some bool
  • some int

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.

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) === '<')
Copy link
Contributor

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?

Copy link
Contributor

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.

Copy link
Contributor

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....

Copy link
Contributor

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.

Copy link
Contributor

@andrepereiradasilva andrepereiradasilva Dec 19, 2016

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.

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 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)
Copy link
Contributor

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?

Copy link
Contributor Author

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) === '<')
Copy link
Contributor

Choose a reason for hiding this comment

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

Same as above

Copy link
Contributor Author

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)
Copy link
Contributor

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?

Copy link
Contributor Author

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))
Copy link
Contributor

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?

Copy link
Contributor Author

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.


// 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()))
Copy link
Contributor

@andrepereiradasilva andrepereiradasilva Dec 19, 2016

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?

Copy link
Contributor Author

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

Copy link
Contributor Author

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?

Copy link
Contributor

@andrepereiradasilva andrepereiradasilva left a 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

@frankmayer frankmayer mentioned this pull request Jan 5, 2017
6 tasks
# Conflicts:
#	libraries/cms/component/router/rules/standard.php
#	libraries/cms/html/string.php
@frankmayer
Copy link
Contributor Author

Conflicts resolved. Pls check and merge.

@frankmayer
Copy link
Contributor Author

@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!!


$prefix = (count($parts) == 3 ? array_shift($parts) : 'JHtml');
$file = (count($parts) == 2 ? array_shift($parts) : '');
$prefix = (count($parts) === 3 ? array_shift($parts) : 'JHtml');
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove parentheses (both lines).


// 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()))
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove parentheses

@andrepereiradasilva
Copy link
Contributor

I have tested this item ✅ successfully on f5fe9bf

code review and according to comments above


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/13276.

@Quy
Copy link
Contributor

Quy commented Jun 12, 2017

I have tested this item ✅ successfully on f5fe9bf

Code review.


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/13276.

@ghost
Copy link

ghost commented Jun 13, 2017

RTC after two successful tests.

@joomla-cms-bot joomla-cms-bot added the RTC This Pull Request is Ready To Commit label Jun 13, 2017
@rdeutz rdeutz merged commit 128e175 into joomla:staging Jun 13, 2017
@joomla-cms-bot joomla-cms-bot removed the RTC This Pull Request is Ready To Commit label Jun 13, 2017
@rdeutz rdeutz added this to the Joomla 3.7.3 milestone Jun 13, 2017
@frankmayer frankmayer deleted the tye-safety-in-libraries-cms-2 branch June 13, 2017 21:56
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.

6 participants