Skip to content

Commit

Permalink
refactor: fix phpstan callable related issues
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubmikita committed Aug 2, 2023
1 parent 6329975 commit 02a343c
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 49 deletions.
9 changes: 5 additions & 4 deletions src/Abstracts/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ public function __call($methodName, $arguments)
return $this->getNotification();
}

return call_user_func_array(
[$this->getNotification(), $methodName],
$arguments
);
$method = [$this->getNotification(), $methodName];

if (is_callable($method)) {
return call_user_func_array($method, $arguments);
}
}

/**
Expand Down
20 changes: 10 additions & 10 deletions src/Admin/ImportExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,13 @@ public function exportRequest()

$type = sanitize_text_field(wp_unslash($_GET['type']));
try {
$data = call_user_func(
[$this, 'prepare' . ucfirst($type) . 'ExportData'],
explode(
',',
sanitize_text_field(wp_unslash($_GET['items'] ?? ''))
$exportMethod = [$this, 'prepare' . ucfirst($type) . 'ExportData'];
$data = is_callable($exportMethod)
? call_user_func(
$exportMethod,
explode(',', sanitize_text_field(wp_unslash($_GET['items'] ?? '')))
)
);
: null;
} catch (\Throwable $e) {
wp_die(
esc_html($e->getMessage()),
Expand Down Expand Up @@ -235,10 +235,10 @@ public function importRequest()
}

try {
$result = call_user_func(
[$this, 'process' . ucfirst($type) . 'ImportRequest'],
$data
);
$processMethod = [$this, 'process' . ucfirst($type) . 'ImportRequest'];
$result = is_callable($processMethod)
? call_user_func($processMethod, $data)
: 'Process method not available';
} catch (\Throwable $e) {
wp_send_json_error($e->getMessage());
}
Expand Down
12 changes: 4 additions & 8 deletions src/Admin/Sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,10 @@ public function ajaxSync()
$ajax = new Response();
$data = $_POST;

$response = method_exists(
$this,
'loadNotificationTo' . ucfirst($data['type'])
)
? call_user_func(
[$this, 'loadNotificationTo' . ucfirst($data['type'])],
$data['hash']
)
$callable = [$this, 'loadNotificationTo' . ucfirst($data['type'])];

$response = method_exists($callable[0], $callable[1]) && is_callable($callable)
? call_user_func($callable, $data['hash'])
: false;

if ($response === false) {
Expand Down
13 changes: 4 additions & 9 deletions src/Core/Upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,13 @@ public function checkUpgrade()

while ($dataVersion < static::$dataVersion) {
$dataVersion++;
$upgradeMethod = 'upgrade_to_v' . $dataVersion;

if (
!method_exists(
$this,
$upgradeMethod
)
) {
$upgradeMethod = [$this, 'upgrade_to_v' . $dataVersion];

if (! method_exists($upgradeMethod[0], $upgradeMethod[1]) || ! is_callable($upgradeMethod)) {
continue;
}

call_user_func([$this, $upgradeMethod]);
call_user_func($upgradeMethod);
}

update_option(
Expand Down
13 changes: 10 additions & 3 deletions src/Defaults/Trigger/WordPress/UpdatesAvailable.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,14 @@ public function mergeTags()
$lists = [];

foreach ($trigger->updateTypes as $updateType) {
if (!$trigger->hasUpdates($updateType)) {
$getUpdatesListMethod = [$trigger, 'get' . ucfirst($updateType) . 'UpdatesList'];

if (!$trigger->hasUpdates($updateType) || ! is_callable($getUpdatesListMethod)) {
continue;
}

$html = '<h3>' . $trigger->getListTitle($updateType) . '</h3>';
$html .= call_user_func([$trigger, 'get' . ucfirst($updateType) . 'UpdatesList']);
$html .= call_user_func($getUpdatesListMethod);
$lists[] = $html;
}

Expand Down Expand Up @@ -378,7 +380,12 @@ public function getThemeUpdatesList()
public function getUpdatesCount($updateType = 'all')
{
if ($updateType !== 'all') {
$updates = call_user_func('get' . ucfirst($updateType) . 'Updates');
$getUpdatesMethod = 'get' . ucfirst($updateType) . 'Updates';

/** @var array<string, mixed> */
$updates = is_callable($getUpdatesMethod)
? call_user_func($getUpdatesMethod)
: [];

if ($updateType === 'core') {
foreach ($updates as $updateKey => $update) {
Expand Down
22 changes: 7 additions & 15 deletions src/Utils/Settings/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,6 @@ public function setRenderer($renderer)
*/
public function setSanitizer($sanitizer)
{

if (!is_callable($sanitizer)) {
throw new \Exception('Field sanitizer is not callable');
}
Expand All @@ -394,11 +393,11 @@ public function setSanitizer($sanitizer)
*/
public function render()
{
if (!is_callable($this->renderer)) {
throw new \Exception('Field rendered is not callable');
}

call_user_func(
$this->renderer,
$this
);
call_user_func($this->renderer, $this);
}

/**
Expand All @@ -409,15 +408,8 @@ public function render()
*/
public function sanitize($value)
{

if ($this->sanitizer) {
$this->value = call_user_func(
$this->sanitizer,
$value,
$this
);
}

return $this->value;
return $this->sanitizer && is_callable($this->sanitizer)
? call_user_func($this->sanitizer, $value, $this)
: $this->value;
}
}

0 comments on commit 02a343c

Please sign in to comment.