Skip to content

Commit

Permalink
Fix: Initialise array before assigning values (#28728)
Browse files Browse the repository at this point in the history
* Qual: Enable PhanUndeclaredVariableDim

# Qual: Enable PhanUndeclaredVariableDim

The notifications are fixed in the code so the detection no longer
needs to be suppressed in the default flow

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Fix: initialise array before adding fields

* Qual: Add type hint to array to avoid phan notification

* Qual: Add type hint to array to avoid phan notification

* Qual: Enable PhanParamSpecial1 (all resolved)

* Qual: Ignore false PhanTypeArraySuspiciousNullable

* Qual: Improve typing to fix PhanTypeArraySuspicious

* Qual: Fix PhanTypeArraySuspicious by replacing null assignment with empty array

* Qual: Fix PhanTypeArraySuspicious by testing as instancof DebugBar

* Qual: Fix PhanTypeComparisonToArray with enhanced typing

* Qual: Fix PhanTypeComparisonToArray with enhanced typing

* Qual: Enable phan messages that no longer appear

* Qual: Exclude phan stubs from codesniffer ruleset
  • Loading branch information
mdeweerd authored Mar 9, 2024
1 parent ce6958e commit 0b19aa4
Show file tree
Hide file tree
Showing 78 changed files with 693 additions and 570 deletions.
1 change: 1 addition & 0 deletions dev/setup/codesniffer/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<!-- info: 'relative' paths are relative to the examined file, so not ok. -->
<exclude-pattern>/build/(html|aps)/</exclude-pattern>
<exclude-pattern>/dev/tools/test/namespacemig/</exclude-pattern>
<exclude-pattern>/dev/tools/phan/stubs/</exclude-pattern>
<!-- <exclude-pattern>dev/initdata/dbf/includes</exclude-pattern> -->
<exclude-pattern>/documents/</exclude-pattern>
<exclude-pattern>/htdocs/core/class/lessc\.class\.php</exclude-pattern>
Expand Down
14 changes: 7 additions & 7 deletions dev/tools/phan/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@
// Dolibarr uses a lot of internal deprecated stuff, not reporting
'PhanDeprecatedProperty',
'PhanDeprecatedFunction',
'PhanCompatibleNegativeStringOffset',
//'PhanCompatibleNegativeStringOffset',
// Dolibarr has quite a few strange noop assignments like $abc=$abc;
'PhanPluginDuplicateExpressionAssignment',
// Nulls are likely mostly false positives
Expand Down Expand Up @@ -379,7 +379,7 @@
'PhanPluginUnknownClosureParamType',
'PhanPluginUnknownClosureReturnType',
// 'PhanPluginNoCommentOnProtectedMethod',
'PhanTypeArraySuspicious',
// 'PhanTypeArraySuspicious',
'PhanTypeMismatchPropertyProbablyReal',
// 'PhanPluginNoCommentOnPrivateMethod',
'PhanPluginUnknownArrayFunctionReturnType',
Expand All @@ -401,7 +401,7 @@
'PhanTypeMismatchDeclaredParamNullable',
'PhanTypeInvalidRightOperandOfAdd',
// 'PhanPluginDescriptionlessCommentOnPrivateProperty',
'PhanUndeclaredVariableDim', // Array initialisation on undeclared var: $abc['x']='ab'
// 'PhanUndeclaredVariableDim', // Array initialisation on undeclared var: $abc['x']='ab'
'PhanTypeInvalidPropertyName',
'PhanPluginDuplicateCatchStatementBody',
'PhanPluginUndeclaredVariableIsset',
Expand Down Expand Up @@ -439,11 +439,11 @@
'PhanPluginBothLiteralsBinaryOp',
// 'PhanTypeMismatchDeclaredParam',
// 'PhanCommentDuplicateMagicMethod',
'PhanParamSpecial1',
// 'PhanParamSpecial1',
'PhanPluginInlineHTMLLeading',
'PhanPluginUseReturnValueInternalKnown',
'PhanRedefinedInheritedInterface',
'PhanTypeComparisonToArray',
//'PhanRedefinedInheritedInterface',
//'PhanTypeComparisonToArray',
'PhanTypeConversionFromArray',
// 'PhanTypeInvalidLeftOperandOfIntegerOp',
'PhanTypeMismatchArgumentInternalProbablyReal',
Expand All @@ -458,7 +458,7 @@
// 'PhanTypeInvalidThrowsIsInterface',
'PhanPluginRedundantAssignmentInLoop',
// 'PhanInvalidCommentForDeclarationType',
'PhanParamSignatureMismatchInternal',
//'PhanParamSignatureMismatchInternal',
// 'PhanPluginEmptyStatementForeachLoop',
// 'PhanCompatibleDimAlternativeSyntax',
'PhanInvalidFQSENInClasslike',
Expand Down
54 changes: 27 additions & 27 deletions htdocs/comm/action/class/actioncomm.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ class ActionComm extends CommonObject
public $priority;

/**
* @var int[] Array of user ids
* @var array<int,array{id:int,transparency:int}> Array of users
*/
public $userassigned = array();

Expand Down Expand Up @@ -488,15 +488,15 @@ public function create(User $user, $notrigger = 0)
if (!is_array($this->userassigned) && !empty($this->userassigned)) { // For backward compatibility when userassigned was an int instead of an array
$tmpid = (int) $this->userassigned;
$this->userassigned = array();
$this->userassigned[$tmpid] = array('id'=>$tmpid, 'transparency'=>$this->transparency);
$this->userassigned[$tmpid] = array('id' => $tmpid, 'transparency' => $this->transparency);
}

$userownerid = $this->userownerid;
$userdoneid = $this->userdoneid;

// Be sure assigned user is defined as an array of array('id'=>,'mandatory'=>,...).
if (empty($this->userassigned) || count($this->userassigned) == 0 || !is_array($this->userassigned)) {
$this->userassigned = array($userownerid=>array('id'=>$userownerid, 'transparency'=>$this->transparency));
$this->userassigned = array($userownerid => array('id' => $userownerid, 'transparency' => $this->transparency));
}

if (!$this->type_id || !$this->type_code) {
Expand Down Expand Up @@ -630,7 +630,7 @@ public function create(User $user, $notrigger = 0)
foreach ($this->userassigned as $key => $val) {
// Common value with new behavior is to have $val = array('id'=>iduser, 'transparency'=>0|1) and $this->userassigned is an array of iduser => $val.
if (!is_array($val)) { // For backward compatibility when $val='id'.
$val = array('id'=>$val);
$val = array('id' => $val);
}

if ($val['id'] > 0) {
Expand Down Expand Up @@ -749,7 +749,7 @@ public function createFromClone(User $fuser, $socid)
if (!$error) {
// Hook of thirdparty module
if (is_object($hookmanager)) {
$parameters = array('objFrom'=>$objFrom);
$parameters = array('objFrom' => $objFrom);
$action = '';
$reshook = $hookmanager->executeHooks('createFrom', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
if ($reshook < 0) {
Expand Down Expand Up @@ -904,14 +904,14 @@ public function fetch($id, $ref = '', $ref_ext = '', $email_msgid = '', $loadres
$this->status = $obj->status;

//email information
$this->email_msgid=$obj->email_msgid;
$this->email_from=$obj->email_from;
$this->email_sender=$obj->email_sender;
$this->email_to=$obj->email_to;
$this->email_tocc=$obj->email_tocc;
$this->email_tobcc=$obj->email_tobcc;
$this->email_subject=$obj->email_subject;
$this->errors_to=$obj->errors_to;
$this->email_msgid = $obj->email_msgid;
$this->email_from = $obj->email_from;
$this->email_sender = $obj->email_sender;
$this->email_to = $obj->email_to;
$this->email_tocc = $obj->email_tocc;
$this->email_tobcc = $obj->email_tobcc;
$this->email_subject = $obj->email_subject;
$this->errors_to = $obj->errors_to;

$this->fetch_optionals();

Expand Down Expand Up @@ -947,20 +947,20 @@ public function fetchResources()
if ($resql) {
// If owner is known, we must but id first into list
if ($this->userownerid > 0) {
$this->userassigned[$this->userownerid] = array('id'=>$this->userownerid); // Set first so will be first into list.
$this->userassigned[$this->userownerid] = array('id' => $this->userownerid); // Set first so will be first into list.
}

while ($obj = $this->db->fetch_object($resql)) {
if ($obj->fk_element > 0) {
switch ($obj->element_type) {
case 'user':
$this->userassigned[$obj->fk_element] = array('id'=>$obj->fk_element, 'mandatory'=>$obj->mandatory, 'answer_status'=>$obj->answer_status, 'transparency'=>$obj->transparency);
$this->userassigned[$obj->fk_element] = array('id' => $obj->fk_element, 'mandatory' => $obj->mandatory, 'answer_status' => $obj->answer_status, 'transparency' => $obj->transparency);
if (empty($this->userownerid)) {
$this->userownerid = $obj->fk_element; // If not defined (should not happened, we fix this)
}
break;
case 'socpeople':
$this->socpeopleassigned[$obj->fk_element] = array('id'=>$obj->fk_element, 'mandatory'=>$obj->mandatory, 'answer_status'=>$obj->answer_status, 'transparency'=>$obj->transparency);
$this->socpeopleassigned[$obj->fk_element] = array('id' => $obj->fk_element, 'mandatory' => $obj->mandatory, 'answer_status' => $obj->answer_status, 'transparency' => $obj->transparency);
break;
}
}
Expand Down Expand Up @@ -994,15 +994,15 @@ public function fetch_userassigned($override = true)
// If owner is known, we must but id first into list
if ($this->userownerid > 0) {
// Set first so will be first into list.
$this->userassigned[$this->userownerid] = array('id'=>$this->userownerid);
$this->userassigned[$this->userownerid] = array('id' => $this->userownerid);
}

while ($obj = $this->db->fetch_object($resql2)) {
if ($obj->fk_element > 0) {
$this->userassigned[$obj->fk_element] = array('id'=>$obj->fk_element,
'mandatory'=>$obj->mandatory,
'answer_status'=>$obj->answer_status,
'transparency'=>$obj->transparency);
$this->userassigned[$obj->fk_element] = array('id' => $obj->fk_element,
'mandatory' => $obj->mandatory,
'answer_status' => $obj->answer_status,
'transparency' => $obj->transparency);
}

if ($override === true) {
Expand Down Expand Up @@ -1245,7 +1245,7 @@ public function update(User $user, $notrigger = 0)
$already_inserted = array();
foreach ($this->userassigned as $key => $val) {
if (!is_array($val)) { // For backward compatibility when val=id
$val = array('id'=>$val);
$val = array('id' => $val);
}
if (!empty($already_inserted[$val['id']])) {
continue;
Expand Down Expand Up @@ -1273,7 +1273,7 @@ public function update(User $user, $notrigger = 0)
$already_inserted = array();
foreach (array_keys($this->socpeopleassigned) as $key => $val) {
if (!is_array($val)) { // For backward compatibility when val=id
$val = array('id'=>$val);
$val = array('id' => $val);
}
if (!empty($already_inserted[$val['id']])) {
continue;
Expand Down Expand Up @@ -1351,7 +1351,7 @@ public function getActions($socid = 0, $fk_element = 0, $elementtype = '', $filt
$parameters = array('sql' => &$sql, 'socid' => $socid, 'fk_element' => $fk_element, 'elementtype' => $elementtype);
$reshook = $hookmanager->executeHooks('getActionsListFrom', $parameters); // Note that $action and $object may have been modified by hook
if (!empty($hookmanager->resPrint)) {
$sql.= $hookmanager->resPrint;
$sql .= $hookmanager->resPrint;
}
$sql .= " WHERE a.entity IN (".getEntity('agenda').")";
if (!empty($socid)) {
Expand All @@ -1375,7 +1375,7 @@ public function getActions($socid = 0, $fk_element = 0, $elementtype = '', $filt
$parameters = array('sql' => &$sql, 'socid' => $socid, 'fk_element' => $fk_element, 'elementtype' => $elementtype);
$reshook = $hookmanager->executeHooks('getActionsListWhere', $parameters); // Note that $action and $object may have been modified by hook
if (!empty($hookmanager->resPrint)) {
$sql.= $hookmanager->resPrint;
$sql .= $hookmanager->resPrint;
}
if ($sortorder && $sortfield) {
$sql .= $this->db->order($sortfield, $sortorder);
Expand Down Expand Up @@ -1847,7 +1847,7 @@ public function getNomUrl($withpicto = 0, $maxlength = 0, $classname = '', $opti

global $action;
$hookmanager->initHooks(array('actiondao'));
$parameters = array('id'=>$this->id, 'getnomurl' => &$result);
$parameters = array('id' => $this->id, 'getnomurl' => &$result);
$reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
if ($reshook > 0) {
$result = $hookmanager->resPrint;
Expand Down Expand Up @@ -2442,7 +2442,7 @@ public function initAsSpecimen()
$this->note_private = "This is a 'private' note.";

$this->userownerid = $user->id;
$this->userassigned[$user->id] = array('id'=>$user->id, 'transparency'=> 1);
$this->userassigned[$user->id] = array('id' => $user->id, 'transparency' => 1);
return 1;
}

Expand Down
2 changes: 1 addition & 1 deletion htdocs/compta/bank/graph.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@
$px1 = null;
$graph_datas = null;
$datas = null;
$datamin = null;
$datamin = array();
$dataall = null;
$labels = null;
$amounts = null;
Expand Down
7 changes: 5 additions & 2 deletions htdocs/contact/consumption.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
* Actions
*/

$parameters = array('id'=>$id);
$parameters = array('id' => $id);
$reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
if ($reshook < 0) {
setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
Expand Down Expand Up @@ -153,6 +153,9 @@
print $object->getCivilityLabel();
print '</td></tr>';

$thirdTypeArray = array();
$elementTypeArray = array();

if (!empty($object->thirdparty->client)) {
$thirdTypeArray['customer'] = $langs->trans("customer");
if (isModEnabled("propal") && $user->hasRight('propal', 'lire')) {
Expand Down Expand Up @@ -370,7 +373,7 @@
$param .= "&type_element=".urlencode($type_element);

$total_qty = 0;
$num=0;
$num = 0;
if ($sql_select) {
$resql = $db->query($sql);
if (!$resql) {
Expand Down
Loading

0 comments on commit 0b19aa4

Please sign in to comment.