Skip to content

More type-safe comparisons in site/templates#13310

Merged
rdeutz merged 9 commits intojoomla:stagingfrom
frankmayer:more-type-safe-comparisons-in-site-templates
Jun 10, 2017
Merged

More type-safe comparisons in site/templates#13310
rdeutz merged 9 commits intojoomla:stagingfrom
frankmayer:more-type-safe-comparisons-in-site-templates

Conversation

@frankmayer
Copy link
Copy Markdown
Contributor

@frankmayer frankmayer commented Dec 20, 2016

Summary of Changes

  • More type-safe comparisons in site/templates

The changes in this PR should be fairly easy to review. They are only type safe int or bool comparisons. This PR is done, so the batch PR #12233 can become a little lighter and easier to review. In hope that this will get merged quickly so further work can be done without conflicting with other PRs. ;)

Testing Instructions

None, should not change behavior

Documentation Changes Required

None.

@ghost
Copy link
Copy Markdown

ghost commented Dec 20, 2016

Could you please explain why we need all these type-safe comparisons?

@frankmayer
Copy link
Copy Markdown
Contributor Author

The quick points:

  • Good coding practice
  • More strict typing = less error prone code
  • Faster processing of such comparisons

There is lots of info on the web on this matter.

@mbabker
Copy link
Copy Markdown
Contributor

mbabker commented Dec 20, 2016

In general, type safety should be preferred, otherwise you run into a lot of funny PHP quirks.

Essentially, '0' == 0 == false == null in PHP with loose typing, same with '1' == 1 == true.

Newer PHP versions also start spitting errors when you give non-numeric types (only integer and float are numeric) to be processed for arithmetic operations, older versions were a little less restrictive and would try to do math on a string representation on the value.

defined('_JEXEC') or die;
$class = ' class="first"';
if (count($this->items[$this->parent->id]) > 0 && $this->maxLevelcat != 0) :
if (count($this->items[$this->parent->id]) > 0 && $this->maxLevelcat !== 0) :
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

are we sure maxLevelcat and maxLevel (below) are always an int?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Contributor Author

@frankmayer frankmayer Dec 21, 2016

Choose a reason for hiding this comment

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

But we should probably type cast those... to be sure... or revert the strict comparison for those cases. I would prefer the prior though...

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

the question is, for instance, $params->get('maxLevelcat', PHP_INT_MAX); could return a string right?
i would prefer the first alternative too

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, unfortunately it could.
I just checked with the JSON data in the menu table and both are strings!!! Why?

Copy link
Copy Markdown
Contributor

@andrepereiradasilva andrepereiradasilva Dec 22, 2016

Choose a reason for hiding this comment

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

Would that be B/C? Not sure.
Also, IMHO, to preserve consistency across the core that would be need to be done across all core int fields in all xml forms and all probaly db tables.
So that would probably need to go to 4.0., not sure tough.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I have already done some thinking on that. Will do some prototyping in the next days.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@frankmayer any news on this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hi @andrepereiradasilva, thanks for the code reviews. Hopefully we can get all the remaining improvements merged this time around 😄
As for your question: I never came around to do some more serious thinking and prototyping. But I agree with you, this might be better suited for 4.0. I will probably reverse this change for now. I think i this should be resolved in a new PR.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@andrepereiradasilva OK, I reversed those strict comparisons for those properties in all related files of this PR. Thanks for pointing that out.

@frankmayer frankmayer mentioned this pull request Jan 6, 2017
4 tasks
<?php echo $this->loadTemplate('items'); ?>

<?php if (!empty($this->children[$this->category->id])&& $this->maxLevel != 0) : ?>
<?php if (!empty($this->children[$this->category->id])&& $this->maxLevel !== 0) : ?>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Add a space before &&.

<?php echo $this->loadTemplate('items'); ?>

<?php if (!empty($this->children[$this->category->id])&& $this->maxLevel != 0) : ?>
<?php if (!empty($this->children[$this->category->id])&& $this->maxLevel !== 0) : ?>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Add a space before &&.

<?php endif; ?>
<?php echo $this->loadTemplate('items'); ?>
<?php if (!empty($this->children[$this->category->id])&& $this->maxLevel != 0) : ?>
<?php if (!empty($this->children[$this->category->id])&& $this->maxLevel !== 0) : ?>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Add a space before &&.

$showleft = ($this->countModules('position-4') or $this->countModules('position-7') or $this->countModules('position-5'));

if ($showRightColumn == 0 and $showleft == 0)
if ($showRightColumn === false and $showleft === false)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Change and to &&.

@andrepereiradasilva
Copy link
Copy Markdown
Contributor

I have tested this item ✅ successfully on 2e806ed

On code review


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

@Quy
Copy link
Copy Markdown
Contributor

Quy commented Jun 1, 2017

I have tested this item ✅ successfully on 2e806ed

Code review.


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

@ghost
Copy link
Copy Markdown

ghost commented Jun 2, 2017

RTC after two successful tests.

@joomla-cms-bot joomla-cms-bot added the RTC This Pull Request is Ready To Commit label Jun 2, 2017
@brianteeman brianteeman modified the milestone: Joomla 4.0 Jun 8, 2017
@rdeutz rdeutz merged commit 1d6f8cd into joomla:staging Jun 10, 2017
@joomla-cms-bot joomla-cms-bot removed the RTC This Pull Request is Ready To Commit label Jun 10, 2017
@rdeutz rdeutz added this to the Joomla 3.7.3 milestone Jun 10, 2017
@frankmayer frankmayer deleted the more-type-safe-comparisons-in-site-templates branch June 12, 2017 10:16
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.

7 participants