code quality improvements + add boolean in a ctive link helper to check if url is active without displaying link

This commit is contained in:
2026-03-17 02:01:07 -07:00
parent 4f996eb7d2
commit 924734b8a4
5 changed files with 22 additions and 15 deletions

View File

@@ -14,7 +14,7 @@
}
],
"require": {
"php": ">=8.1",
"php": ">=8.2",
"cakephp/cakephp": "^5.0"
},
"require-dev": {

View File

@@ -8,3 +8,5 @@ parameters:
bootstrapFiles:
- tests/bootstrap.php
treatPhpDocTypesAsCertain: false
ignoreErrors:
- identifier: trait.unused

View File

@@ -3,7 +3,7 @@ declare(strict_types=1);
namespace CheeseCake\Controller;
use App\Controller\AppController as BaseController;
use Cake\Controller\Controller;
class AppController extends BaseController {
class AppController extends Controller {
}

View File

@@ -5,12 +5,15 @@ namespace CheeseCake\View\Helper;
use Cake\Routing\Router;
use Cake\View\Helper;
use Cake\View\Helper\HtmlHelper;
/**
* ActiveLink helper
*
* @property HtmlHelper $Html
*/
class ActiveLinkHelper extends Helper {
/**
* Default configuration.
*
@@ -42,12 +45,12 @@ class ActiveLinkHelper extends Helper {
if (!array_key_exists('target', $options) || !$currentUrl) {
return $this->Html->link($title, $url, $options);
}
$target = $options['target'];
$activeClass = $options['activeClass'] ?? $this->getConfig('activeClass');
unset($options['target']);
unset($options['activeClass']);
$activeClass = $options['activeClass'] ?? $this->getConfig('activeClass');
unset($options['target']);
unset($options['activeClass']);
if (is_string($target)) {
return $this->_linkFromStringTarget($currentUrl, $target, $title, $url, $activeClass, $options);
}
@@ -78,7 +81,7 @@ class ActiveLinkHelper extends Helper {
}
$options['class'] = $this->_addClass($options, $activeClass);
return $this->Html->link($title, $url, $options);
}
@@ -113,18 +116,20 @@ class ActiveLinkHelper extends Helper {
/**
* @param array $providedOptions
* @param string $toAdd
*
* @return string
*/
protected function _addClass(array $providedOptions, string $toAdd): string {
return array_key_exists('class', $providedOptions) ? $providedOptions['class'] . ' ' . $toAdd : $toAdd;
}
/**
/**
* @param array $current
* @param string $targetString
* @param string $title
* @param array|string|null $url
* @param string $activeClass
* @param array $options
*
* @return string
@@ -138,8 +143,8 @@ class ActiveLinkHelper extends Helper {
return $this->Html->link($title, $url, $options);
}
/**
/**
* @param array $current
* @param array $targetUrl
*

View File

@@ -108,7 +108,7 @@ class ActiveLinkHelperTest extends TestCase {
// matches specific controller action
$specificActionResult = $helper->link('goto', ['prefix' => 'Admin', 'controller' => 'Users', 'action' => 'index'], [
'target' => ['controller' => 'Events', 'action' => 'index'],
'current' => ['prefix' => 'Admin', 'controller' => 'Users', 'action' => 'index'],
'current' => ['prefix' => 'Admin', 'controller' => 'Users', 'action' => 'index'],
'activeClass' => 'awesome-active-class',
'class' => 'link-class',
]);