Skip to content

Commit

Permalink
Cleanup code and improve PHPDoc
Browse files Browse the repository at this point in the history
  • Loading branch information
adpe committed Mar 19, 2021
1 parent c31c98d commit 9f1f781
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
26 changes: 11 additions & 15 deletions externallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ public static function get_information_parameters() {
/**
* Returns plugins information.
*
* @param string $type
* @param int|null $contribonly
* @return string
* @param string|null $type The plugin type.
* @param int|null $contribonly Include contributed plugins.
* @return array|\core\plugininfo\base[]|string
* @throws dml_exception
* @throws invalid_parameter_exception
* @throws required_capability_exception
*/
public static function get_information($type, $contribonly) {
public static function get_information(string $type, ?int $contribonly) {
$syscontext = context_system::instance();
require_capability('moodle/site:config', $syscontext);

Expand All @@ -75,25 +75,21 @@ public static function get_information($type, $contribonly) {
if (!empty($params['type'])) {
if (!empty($params['contribonly'])) {
// Get additional plugins by type and contrib.
$plugininfo = self::get_plugins_by_parameters($pluginman, $type, false);
$plugininfo = self::get_plugins_by_parameters($pluginman, $type);
} else {
// Get all plugins by type.
$plugininfo = $pluginman->get_plugins_of_type($type);
}
} else {
if (!empty($params['contribonly'])) {
// Get all plugins by contrib.
$plugininfo = self::get_plugins_by_parameters($pluginman, null, false);
$plugininfo = self::get_plugins_by_parameters($pluginman);
} else {
// Get all plugins.
$plugininfo = self::get_plugins_by_parameters($pluginman, null, true);
}
}

if (empty($plugininfo)) {
return array();
}

return $plugininfo;
}

Expand All @@ -118,18 +114,18 @@ public static function get_information_returns() {
/**
* Retrieves plugin data based on type and contrib.
*
* @param object $pluginman
* @param string $type
* @param bool $all
* @param object $pluginman The core plugin manager singleton instance.
* @param string|null $type The plugin type.
* @param bool $contribonly Include contributed plugins.
* @return array
*/
protected static function get_plugins_by_parameters($pluginman, $type = null, $all = false) {
protected static function get_plugins_by_parameters(object $pluginman, $type = null, $contribonly = false): array {
$plugins = array();
$plugininfo = $pluginman->get_plugins();

foreach ($plugininfo as $plugintype => $pluginnames) {
foreach ($pluginnames as $pluginname => $plugin) {
if ($all || ($plugin->type == $type && !$plugin->is_standard()) ||
if ($contribonly || ($plugin->type == $type && !$plugin->is_standard()) ||
(is_null($type) && !$plugin->is_standard())) {
$key = $plugintype . '_' . $pluginname;
$plugins[$key] = $plugin;
Expand Down
1 change: 1 addition & 0 deletions tests/externallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

/**
* Class local_pluginsfetcher_external_testcase.
* @covers local_pluginsfetcher_external
*/
class local_pluginsfetcher_external_testcase extends externallib_advanced_testcase {
/**
Expand Down

0 comments on commit 9f1f781

Please sign in to comment.