Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Resolving possible Undefined variable notice $currentAssetId #1765

Merged
merged 3 commits into from

5 participants

@betweenbrain

Undefined variable: currentAssetId in /libraries/joomla/table/table.php on line 778 may occur, please see https://github.com/JTracker/jissues/issues/44 for the related issue for discovery

@elinw

Can't you have an empty $this->assetid when there is no $currentAssetId? In fact won't that always happen? https://github.com/betweenbrain/joomla-platform/blob/a1d8d1a1db9e09f6d6de48930af758213ee127b1/libraries/joomla/table/table.php#L698

Maybe better either to actually define$currentAssetId for the other cases or to add a check for it being set to the second condition.

@mbabker
Owner

$currentAssetId isn't always set though, and that's the issue he's trying to resolve here.

@pasamio

Why not on line 697 just default it to zero to ensure it is always set and if there is an asset ID in the object then it will be updated but it is always defined. Perhaps it is a habit I have left over from languages where if you define something in a block that it isn't available externally but I prefer this and it is only an extra line to add. Perhaps I am missing a nuance here but would that solve the problem as well?

@elinw

Exactly, setting it for every case makes more sense then eliminating the possibility of having the first condition of the block he's modifying never be met which defeats half the point of the code in the block. Or as I said the other possibility is to add isset to the second condition. It's most likely less confusing for codebrowsers to set it for everyone though.

libraries/joomla/table/table.php
((4 lines not shown))
- // Create an asset_id or heal one that is corrupted.
- if (empty($this->asset_id) || ($currentAssetId != $this->asset_id && !empty($this->asset_id)))
- {
- // Update the asset_id field in this table.
- $this->asset_id = (int) $asset->id;
-
- $query = $this->_db->getQuery(true);
- $query->update($this->_db->quoteName($this->_tbl));
- $query->set('asset_id = ' . (int) $this->asset_id);
- $this->appendPrimaryKeys($query);
- $this->_db->setQuery($query);
-
- $this->_db->execute();
+ if (isset($currentAssetId)) {
+ // Create an asset_id or heal one that is corrupted.
+ if (empty($this->asset_id) || ($currentAssetId != $this->asset_id && !empty($this->asset_id))) {
@elinw
elinw added a note

if (empty($this->asset_id) || (isset($currentAssetId) && $currentAssetId != $this->asset_id && !empty($this->asset_id)))

would be one option here.

or you could add
else
{
$currentAssetId = 0
}

where $currentAssetId is set for the cases where $this->asset_id is !empty. Or just put it before and save 3 lines of code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@betweenbrain

Michael is exactly right in that the issue I'm trying to solve is that $currentAssetId isn't always set with the existing code. My approach was to change the existing code as little as possible. That said, Sam's solution of setting it to 0 on line 697 is a great way to approach it and would actually simplify line 779. If that's the approach the maintainers would prefer, I'd be happy to update my PR.

@mbabker
Owner

Looks to be a better solution. Now we just need to get the codestyle fixed, and I think we're good to go. http://developer.joomla.org/pulls/pulls/1765.html

@elinw

Yes I believe that is exactly what I said in my first reply. It's always set when the first condition is not true but it makes sense to do it because who knows what could happen in the intervening lines.

@betweenbrain
@ianmacl ianmacl merged commit 19480ec into joomla:staging
@betweenbrain

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 18, 2012
  1. @betweenbrain
Commits on Dec 19, 2012
  1. @betweenbrain
Commits on Dec 20, 2012
  1. @betweenbrain

    Code formatting changes to resolve all code formatting errors reporte…

    betweenbrain authored
    …d by PHP Codesniffer using joomla / coding-standards from https://github.com/joomla/coding-standards
This page is out of date. Refresh to see the latest.
Showing with 20 additions and 16 deletions.
  1. +20 −16 libraries/joomla/table/table.php
View
36 libraries/joomla/table/table.php
@@ -20,7 +20,7 @@
* @subpackage Table
* @link http://docs.joomla.org/JTable
* @since 11.1
- * @tutorial Joomla.Platform/jtable.cls
+ * @tutorial Joomla.Platform/jtable.cls
*/
abstract class JTable extends JObject
{
@@ -181,7 +181,7 @@ public function getFields()
if ($cache === null)
{
// Lookup the fields for this table only once.
- $name = $this->_tbl;
+ $name = $this->_tbl;
$fields = $this->_db->getTableColumns($name, false);
if (empty($fields))
@@ -211,7 +211,7 @@ public function getFields()
public static function getInstance($type, $prefix = 'JTable', $config = array())
{
// Sanitize and prepare the table class name.
- $type = preg_replace('/[^A-Z0-9_\.-]/i', '', $type);
+ $type = preg_replace('/[^A-Z0-9_\.-]/i', '', $type);
$tableClass = $prefix . ucfirst($type);
// Only try to load the class if it doesn't already exist.
@@ -432,6 +432,7 @@ public function getKeyName($multiple = false)
return $this->_tbl_keys[0];
}
}
+
return '';
}
@@ -591,12 +592,12 @@ public function load($keys = null, $reset = true)
if (empty($keys))
{
$empty = true;
- $keys = array();
+ $keys = array();
// If empty, use the value of the current key
foreach ($this->_tbl_keys as $key)
{
- $empty = $empty && empty($this->$key);
+ $empty = $empty && empty($this->$key);
$keys[$key] = $this->$key;
}
@@ -695,6 +696,8 @@ public function store($updateNulls = false)
{
$k = $this->_tbl_keys;
+ $currentAssetId = 0;
+
if (!empty($this->asset_id))
{
$currentAssetId = $this->asset_id;
@@ -732,8 +735,8 @@ public function store($updateNulls = false)
*/
$parentId = $this->_getAssetParentId();
- $name = $this->_getAssetName();
- $title = $this->_getAssetTitle();
+ $name = $this->_getAssetName();
+ $title = $this->_getAssetTitle();
$asset = self::getInstance('Asset', 'JTable', array('dbo' => $this->getDbo()));
$asset->loadByName($name);
@@ -759,8 +762,8 @@ public function store($updateNulls = false)
// Prepare the asset to be stored.
$asset->parent_id = $parentId;
- $asset->name = $name;
- $asset->title = $title;
+ $asset->name = $name;
+ $asset->title = $title;
if ($this->_rules instanceof JAccessRules)
{
@@ -807,7 +810,7 @@ public function store($updateNulls = false)
*
* @return boolean True on success.
*
- * @link http://docs.joomla.org/JTable/save
+ * @link http://docs.joomla.org/JTable/save
* @since 11.1
*/
public function save($src, $orderingFilter = '', $ignore = '')
@@ -891,7 +894,7 @@ public function delete($pk = null)
if ($this->_trackAssets)
{
// Get the asset name
- $name = $this->_getAssetName();
+ $name = $this->_getAssetName();
$asset = self::getInstance('Asset');
if ($asset->loadByName($name))
@@ -987,7 +990,7 @@ public function checkOut($userId, $pk = null)
$this->_db->execute();
// Set table values in the object.
- $this->checked_out = (int) $userId;
+ $this->checked_out = (int) $userId;
$this->checked_out_time = $time;
return true;
@@ -1048,7 +1051,7 @@ public function checkIn($pk = null)
$this->_db->execute();
// Set table values in the object.
- $this->checked_out = 0;
+ $this->checked_out = 0;
$this->checked_out_time = '';
return true;
@@ -1245,6 +1248,7 @@ public function getPrimaryKey(array $keys = array())
}
}
}
+
return $keys;
}
@@ -1336,8 +1340,8 @@ public function move($delta, $where = '')
return true;
}
- $k = $this->_tbl_key;
- $row = null;
+ $k = $this->_tbl_key;
+ $row = null;
$query = $this->_db->getQuery(true);
// Select the primary key and ordering values from the table.
@@ -1434,7 +1438,7 @@ public function publish($pks = null, $state = 1, $userId = 0)
}
$userId = (int) $userId;
- $state = (int) $state;
+ $state = (int) $state;
// If there are no primary keys set check to see if the instance key is set.
if (empty($pks))
Something went wrong with that request. Please try again.