Skip to content

Commit

Permalink
Add PHPStan level 0 checks
Browse files Browse the repository at this point in the history
* Add PHPStan level 0 check
* Fix: Method NotificationSettingConfig::update() should return bool but return statement is missing.
* Fix: Method Html::showDatesTimelineGraph() should return array but return statement is missing.
* Fix: Array has 2 duplicate keys with value 'multiple' ('multiple', 'multiple').
* Fix: Access to an undefined property HTMLTableMain::$title.
* Fix: Access to an undefined property HTMLTableGroup::$ordered_headers.
* Fix: Variable $subheader_name is undefined
* Fix: Access to an undefined property HTMLTableCell::$numberOfLines.
* Fix: Access to an undefined property HTMLTableCell::$start.
* Fix: Call to function unset() contains undefined variable $name|$type|$cbbox|$cidinfo|$cw|$dw.
* Fix: Method GLPIMailer::setLanguage() should return bool but return statement is missing.
* Fix FieldUnicity display when there is no fields selected
* Fix: Method Glpi\Features\PlanningEvent::getAlreadyPlannedInformation() should return string but return statement is missing.
* Fix: Call to an undefined static method PlanningExternalEventTemplate::getVisibilityCriteria().
* Fix: Instantiated class CommonITILTask is abstract.
* Fix: Undefined variable: $options
* Fix: Method DropdownTranslation::regenerateAllCompletenameTranslationsFor() should return string but return statement is missing.
* Fix: Method Dropdown::showFrequency() should return int|string but return statement is missing.
* Fix: Method Domain_Item::showForItem() should return bool but return statement is missing.
* Remove Domain::getAdditionalFields() (Domain does not inherits CommonDropdown anymore)
* Fix: Method Document_Item::showAddFormForItem() should return bool but return statement is missing.
* Fix: Method Device*::getHTMLTableHeader() should return HTMLTableHeader but return statement is missing.
* Fix: Undefined variable: $RELATION
* Fix: Method DatabaseInstance::getTabNameForItem() should return string but return statement is missing.
* Fix: Method DCRoom::getTabNameForItem() should return string but return statement is missing.
* Fix: Result of method DBmysqlIterator::handleFields() (void) is used.
* Fix: Method DBmysql::rollBack() should return bool but return statement is missing.
* Fix: Method CronTask::cronInfo() should return array but return statement is missing.
* Fix rights checks on CronTask massive actions
* Fix: Result of method CronTask::showHistoryDetail() (void) is used.
* Fix: Method Contact::getAddress() should return string but return statement is missing.
* Fix: Method Consumable::getStatus() should return string but return statement is missing.
* Fix: Access to undefined constant Glpi\Console\Migration\UnsignedKeysCommand::ERROR_UNABLE_TO_UPDATE_CONFIG.
* Fix: Class DB not found.
* Fix: Undefined variable: $parent
* Fix: Undefined variable: $title
* Fix: Method CommonGLPI::canUpdate() should return bool but return statement is missing.
* Fix: Call to an undefined static method static(CommonGLPI)::getIcon().
* Fix: Call to an undefined method $this(CommonGLPI)::isField().
* Fix: Call to an undefined method CommonGLPI::getNameID().
* Fix: Call to an undefined method CommonGLPI::getFromDB().
* Fix: Access to an undefined property CommonGLPI::$fields.
* Fix: Call to an undefined method CommonGLPI::getLink().
* Fix: Method CommonGLPI::getErrorMessage() should return string but return statement is missing.
* Fix: Access to an undefined property CommonGLPI::$fields.
* Fix: Access to an undefined property CommonDBVisible::$entities|groups|profiles|users.
* Fix Glpi\Features\PlanningEvent::post_getEmpty()
* Fix: Parameter #1 $position (int) of method DBmysqlIterator::seek() is not contravariant with parameter #1 $offset (mixed) of method SeekableIterator::seek().
* Fix: Call to an undefined static method static(CommonDBTM)::getDcBreadcrumbSpecificValueToDisplay().
* Fix: Access to an undefined property CommonDBTM::$itemtype.
* Fix: Result of method CommonDBTM::canGlobal() (void) is used.
* Fix: Result of method CommonDBTM::updateInDB() (void) is used.
* Fix: Call to an undefined method CommonDBTM::clone().
* Fix: Method CommonDBTM::restoreSavedValues() should return array but return statement is missing.
* Fix: Method Certificate_Item::showForItem() should return bool but return statement is missing.
* Fix: Result of static method Contract::showCentral() (void) is used.
* Fix: Method Cartridge::uninstall() should return bool but return statement is missing.
* Fix: Method AuthLDAP::ldapImportUserByServerId() should return array|bool but return statement is missing.
* Fix: Unsafe usage of new static().
* Fix: Function brotli_compress|brotli_uncompress not found.
  • Loading branch information
cedric-anne authored Sep 13, 2022
1 parent 15a89c0 commit 938a234
Show file tree
Hide file tree
Showing 47 changed files with 480 additions and 210 deletions.
7 changes: 7 additions & 0 deletions .github/actions/lint_php-lint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,10 @@ touch ~/phpcs.cache
vendor/bin/phpcs \
--cache ~/phpcs.cache \
.

echo "Run code static analysis"
vendor/bin/phpstan analyze \
--ansi \
--memory-limit=2G \
--no-interaction \
--no-progress
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"maglnet/composer-require-checker": "^3.5",
"mikey179/vfsstream": "^1.6",
"php-parallel-lint/php-parallel-lint": "^1.3",
"phpstan/phpstan": "^1.8",
"squizlabs/php_codesniffer": "^3.7"
},
"replace": {
Expand Down
78 changes: 69 additions & 9 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion inc/based_config.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
*/

if (!defined('GLPI_ROOT')) {
die("Sorry. You can't access this file directly");
define('GLPI_ROOT', dirname(__FILE__, 2));
}

// Notice problem for date function :
Expand Down
14 changes: 14 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
parameters:
level: 0
bootstrapFiles:
- inc/based_config.php
paths:
- src
scanDirectories:
- inc
ignoreErrors:
- '/Call to static method \w+\(\) on an unknown class phpCAS/'
- '/Class Ldap\\Connection not found/'
- '/Instantiated class (DB|DBSlave) not found/'
- '/Instantiated class XHProfRuns_Default not found/'
- '/\w+ has been replaced by \w+/'
18 changes: 16 additions & 2 deletions src/Agent/Communication/AbstractRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,14 @@ public function handleRequest($data): bool
$data = gzdecode($data);
break;
case self::COMPRESS_BR:
$data = brotli_uncompress($data);
if (!function_exists('brotli_uncompress')) {
trigger_error(
'Brotli PHP extension is required to handle Brotli compression algorithm in inventory feature.',
E_USER_WARNING
);
} else {
$data = brotli_uncompress($data);
}
break;
case self::COMPRESS_DEFLATE:
$data = gzinflate($data);
Expand Down Expand Up @@ -521,7 +528,14 @@ public function getResponse(): string
$data = gzencode($data);
break;
case self::COMPRESS_BR:
$data = brotli_compress($data);
if (!function_exists('brotli_compress')) {
trigger_error(
'Brotli PHP extension is required to handle Brotli compression algorithm in inventory feature.',
E_USER_WARNING
);
} else {
$data = brotli_compress($data);
}
break;
case self::COMPRESS_DEFLATE:
$data = gzdeflate($data);
Expand Down
2 changes: 1 addition & 1 deletion src/Application/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public static function getInstance(): ErrorHandler

if ($instance === null) {
global $PHPLOGGER;
$instance = new static($PHPLOGGER);
$instance = new self($PHPLOGGER);
}

return $instance;
Expand Down
2 changes: 1 addition & 1 deletion src/Application/View/TemplateRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public static function getInstance(): TemplateRenderer
static $instance = null;

if ($instance === null) {
$instance = new static();
$instance = new self();
}

return $instance;
Expand Down
3 changes: 1 addition & 2 deletions src/AuthLDAP.php
Original file line number Diff line number Diff line change
Expand Up @@ -2851,9 +2851,8 @@ public static function ldapImportUserByServerId(
ErrorHandler::getInstance()->handleException($e);
return false;
}
} else {
return false;
}
return false;
}


Expand Down
2 changes: 1 addition & 1 deletion src/Cartridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,8 @@ public function uninstall($ID)

return true;
}
return false;
}
return false;
}


Expand Down
2 changes: 2 additions & 0 deletions src/Certificate_Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -541,5 +541,7 @@ public static function showForItem(CommonDBTM $item, $withtemplate = 0)
Html::closeForm();
}
echo "</div>";

return true;
}
}
18 changes: 11 additions & 7 deletions src/CommonDBTM.php
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ public function getLogTypeID()
* @param string[] $updates fields to update
* @param string[] $oldvalues array of old values of the updated fields
*
* @return void
* @return bool
**/
public function updateInDB($updates, $oldvalues = [])
{
Expand Down Expand Up @@ -1185,7 +1185,7 @@ protected function restoreInput(array $default = [])
*
* @param array $saved Array of values saved in session
*
* @return array Array of values
* @return void
**/
protected function restoreSavedValues(array $saved = [])
{
Expand Down Expand Up @@ -1229,7 +1229,10 @@ public function add(array $input, $options = [], $history = true)
}

// This means we are not adding a cloned object
if (!Toolbox::hasTrait($this, \Glpi\Features\Clonable::class) || !isset($input['clone'])) {
if (
(!Toolbox::hasTrait($this, \Glpi\Features\Clonable::class) || !isset($input['clone']))
&& method_exists($this, 'clone')
) {
// This means we are asked to clone the object (old way). This will clone the clone method
// that will set the clone parameter to true
if (isset($input['_oldID'])) {
Expand Down Expand Up @@ -3050,7 +3053,7 @@ public function checkGlobal($right)
*
* @param mixed $right Right to check : c / r / w / d / READ / UPDATE / CREATE / DELETE
*
* @return void
* @return bool
**/
public function canGlobal($right)
{
Expand Down Expand Up @@ -4337,7 +4340,7 @@ public function getUnicityErrorMessage($msgs, $unicity, $doubles)
$message_text .= '<br>' . __('Other item exist');

foreach ($doubles as $double) {
if (in_array('CommonDBChild', class_parents($this))) {
if ($this instanceof CommonDBChild) {
if ($this->getField($this->itemtype)) {
$item = new $double['itemtype']();
} else {
Expand Down Expand Up @@ -4613,8 +4616,9 @@ public static function getSpecificValueToDisplay($field, $values, array $options

switch ($field) {
case '_virtual_datacenter_position':
if (method_exists(static::class, 'getDcBreadcrumbSpecificValueToDisplay')) {
return static::getDcBreadcrumbSpecificValueToDisplay($values['id']);
$static = new static();
if (method_exists($static, 'getDcBreadcrumbSpecificValueToDisplay')) {
return $static::getDcBreadcrumbSpecificValueToDisplay($values['id']);
}
}

Expand Down
70 changes: 70 additions & 0 deletions src/CommonDBVisible.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,76 @@
*/
abstract class CommonDBVisible extends CommonDBTM
{
/**
* Entities on which item is visible.
* Keys are ID, values are DB fields values.
* @var array
*/
protected $entities = [];

/**
* Groups for whom item is visible.
* Keys are ID, values are DB fields values.
* @var array
*/
protected $groups = [];

/**
* Profiles for whom item is visible.
* Keys are ID, values are DB fields values.
* @var array
*/
protected $profiles = [];

/**
* Users for whom item is visible.
* Keys are ID, values are DB fields values.
* @var array
*/
protected $users = [];

public function __get(string $property)
{
// TODO Deprecate access to variables in GLPI 10.1.
$value = null;
switch ($property) {
case 'entities':
case 'groups':
case 'profiles':
case 'users':
$value = $this->$property;
break;
default:
$trace = debug_backtrace();
trigger_error(
sprintf('Undefined property: %s::%s in %s on line %d', __CLASS__, $property, $trace[0]['file'], $trace[0]['line']),
E_USER_WARNING
);
break;
}
return $value;
}

public function __set(string $property, $value)
{
// TODO Deprecate access to variables in GLPI 10.1.
switch ($property) {
case 'entities':
case 'groups':
case 'profiles':
case 'users':
$this->$property = $value;
break;
default:
$trace = debug_backtrace();
trigger_error(
sprintf('Undefined property: %s::%s in %s on line %d', __CLASS__, $property, $trace[0]['file'], $trace[0]['line']),
E_USER_WARNING
);
break;
}
}

/**
* Is the login user have access to item based on visibility configuration
*
Expand Down
2 changes: 1 addition & 1 deletion src/CommonDevice.php
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ public static function getNameField()
* (default NULL)
* @param $options array parameter such as restriction
*
* @return HTMLTableHeader
* @return HTMLTableHeader|void
**/
public static function getHTMLTableHeader(
$itemtype,
Expand Down
Loading

0 comments on commit 938a234

Please sign in to comment.