<commit_before><?php
/**
 * Copyright (c) Qobo Ltd. (https://www.qobo.biz)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Qobo Ltd. (https://www.qobo.biz)
 * @license       https://opensource.org/licenses/mit-license.php MIT License
 */
namespace Menu\View\Cell;

use Cake\Datasource\EntityInterface;
use Cake\Event\Event;
use Cake\View\Cell;
use InvalidArgumentException;
use Menu\Event\EventName;

class MenuCell extends Cell
{
    /**
     * Default menu renderer class
     */
    const DEFAULT_RENDERER = 'Menu\\MenuBuilder\\SystemMenuRenderAdminLte';

    /**
     * Module type identifier
     */
    const TYPE_MODULE = 'module';

    /**
     * Current session user.
     *
     * @var array
     */
    protected $user = [];

    /**
     * Full-base URL flag.
     *
     * @var boolean
     */
    protected $fullBaseUrl = false;

    /**
     * Menu item defaults.
     *
     * @var array
     */
    protected $_defaults = [
        'url' => '#',
        'label' => 'Undefined',
        'icon' => 'circle-o',
        'order' => 0,
        'target' => '_self',
        'children' => [],
        'desc' => ''
    ];

    /**
     * Default display method.
     *
     * Retrieves menu items and rendering format and passes them to the Cell View.
     *
     * @param string $name Menu name
     * @param array $user User info
     * @param bool $fullBaseUrl Full-base URL flag
     * @param string $renderer Menu renderer class name
     * @return void
     */
    public function display($name, array $user, $fullBaseUrl = false, $renderer = null)
    {
        // validate menu name
        $this->_validateName($name);

        if (is_null($renderer)) {
            $renderer = static::DEFAULT_RENDERER;
        }

        $this->loadModel('Menu.Menus');

        $this->user = $user;
        $this->fullBaseUrl = (bool)$fullBaseUrl;

        // get menu
        $menu = $this->Menus->findByName($name)->firstOrFail();

        $menuItems = $menu->default ?
            $this->_getMenuItemsFromEvent($menu) :
            $this->_getMenuItemsFromTable($menu);

        $menuItems = $this->_normalizeItems($menuItems);

        if ($menu->default) {
            $menuItems = $this->_sortItems($menuItems);
        }

        $this->set('menuItems', $menuItems);
        $this->set('user', $this->user);
        $this->set('name', $name);
        $this->set('renderer', $renderer);
    }

    /**
     * Menu items getter using Event.
     *
     * @param \Cake\Datasource\EntityInterface $menu Menu entity
     * @param array $modules Modules to fetch menu items for
     * @return array
     */
    protected function _getMenuItemsFromEvent(EntityInterface $menu, array $modules = [])
    {
        $event = new Event((string)EventName::GET_MENU_ITEMS(), $this, [
            'name' => $menu->name,
            'user' => $this->user,
            'fullBaseUrl' => $this->fullBaseUrl,
            'modules' => $modules
        ]);
        $this->eventManager()->dispatch($event);

        return $event->result ? $event->result : [];
    }

    /**
     * Menu items getter using database table.
     *
     * @param \Cake\Datasource\EntityInterface $menu Menu entity
     * @return array
     */
    protected function _getMenuItemsFromTable(EntityInterface $menu)
    {
        $this->loadModel('Menu.MenuItems');

        $query = $this->MenuItems->find('threaded', [
            'conditions' => ['MenuItems.menu_id' => $menu->id],
            'order' => ['MenuItems.lft']
        ]);

        if ($query->isEmpty()) {
            return [];
        }

        $result = [];
        $count = 0;
        foreach ($query->all() as $entity) {
            $item = $this->_getMenuItem($menu, $entity->toArray(), ++$count);

            if (empty($item)) {
                continue;
            }
            $result[] = $item;
        }

        return $result;
    }

    /**
     * Menu item getter.
     *
     * @param \Cake\Datasource\EntityInterface $menu Menu entity
     * @param array $item Menu item
     * @return array
     */
    protected function _getMenuItem(EntityInterface $menu, array $item, $order = 0)
    {
        if (empty($item)) {
            return [];
        }

        $label = $item['label'];
        $icon = $item['icon'];
        $type = $item['type'];

        $children = !empty($item['children']) ? $item['children'] : [];

        if (!empty($children)) {
            $count = 0;
            foreach ($children as $key => $child) {
                $children[$key] = $this->_getMenuItem($menu, $child, ++$count);
            }
        }

        if (static::TYPE_MODULE === $type) {
            $item = $this->_getMenuItemsFromEvent($menu, [$item['url']]);
            $item = current($item);
        }

        if (!empty($children)) {
            $item['children'] = $children;
        }

        if (empty($item)) {
            return [];
        }

        if (static::TYPE_MODULE === $type) {
            $item['label'] = $label;
            $item['icon'] = $icon;
        }
<commit_msg>Reset array before accessing current index, fixes issue with PHP 5.6<commit_after><?php
/**
 * Copyright (c) Qobo Ltd. (https://www.qobo.biz)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Qobo Ltd. (https://www.qobo.biz)
 * @license       https://opensource.org/licenses/mit-license.php MIT License
 */
namespace Menu\View\Cell;

use Cake\Datasource\EntityInterface;
use Cake\Event\Event;
use Cake\View\Cell;
use InvalidArgumentException;
use Menu\Event\EventName;

class MenuCell extends Cell
{
    /**
     * Default menu renderer class
     */
    const DEFAULT_RENDERER = 'Menu\\MenuBuilder\\SystemMenuRenderAdminLte';

    /**
     * Module type identifier
     */
    const TYPE_MODULE = 'module';

    /**
     * Current session user.
     *
     * @var array
     */
    protected $user = [];

    /**
     * Full-base URL flag.
     *
     * @var boolean
     */
    protected $fullBaseUrl = false;

    /**
     * Menu item defaults.
     *
     * @var array
     */
    protected $_defaults = [
        'url' => '#',
        'label' => 'Undefined',
        'icon' => 'circle-o',
        'order' => 0,
        'target' => '_self',
        'children' => [],
        'desc' => ''
    ];

    /**
     * Default display method.
     *
     * Retrieves menu items and rendering format and passes them to the Cell View.
     *
     * @param string $name Menu name
     * @param array $user User info
     * @param bool $fullBaseUrl Full-base URL flag
     * @param string $renderer Menu renderer class name
     * @return void
     */
    public function display($name, array $user, $fullBaseUrl = false, $renderer = null)
    {
        // validate menu name
        $this->_validateName($name);

        if (is_null($renderer)) {
            $renderer = static::DEFAULT_RENDERER;
        }

        $this->loadModel('Menu.Menus');

        $this->user = $user;
        $this->fullBaseUrl = (bool)$fullBaseUrl;

        // get menu
        $menu = $this->Menus->findByName($name)->firstOrFail();

        $menuItems = $menu->default ?
            $this->_getMenuItemsFromEvent($menu) :
            $this->_getMenuItemsFromTable($menu);

        $menuItems = $this->_normalizeItems($menuItems);

        if ($menu->default) {
            $menuItems = $this->_sortItems($menuItems);
        }

        $this->set('menuItems', $menuItems);
        $this->set('user', $this->user);
        $this->set('name', $name);
        $this->set('renderer', $renderer);
    }

    /**
     * Menu items getter using Event.
     *
     * @param \Cake\Datasource\EntityInterface $menu Menu entity
     * @param array $modules Modules to fetch menu items for
     * @return array
     */
    protected function _getMenuItemsFromEvent(EntityInterface $menu, array $modules = [])
    {
        $event = new Event((string)EventName::GET_MENU_ITEMS(), $this, [
            'name' => $menu->name,
            'user' => $this->user,
            'fullBaseUrl' => $this->fullBaseUrl,
            'modules' => $modules
        ]);
        $this->eventManager()->dispatch($event);

        return $event->result ? $event->result : [];
    }

    /**
     * Menu items getter using database table.
     *
     * @param \Cake\Datasource\EntityInterface $menu Menu entity
     * @return array
     */
    protected function _getMenuItemsFromTable(EntityInterface $menu)
    {
        $this->loadModel('Menu.MenuItems');

        $query = $this->MenuItems->find('threaded', [
            'conditions' => ['MenuItems.menu_id' => $menu->id],
            'order' => ['MenuItems.lft']
        ]);

        if ($query->isEmpty()) {
            return [];
        }

        $result = [];
        $count = 0;
        foreach ($query->all() as $entity) {
            $item = $this->_getMenuItem($menu, $entity->toArray(), ++$count);

            if (empty($item)) {
                continue;
            }
            $result[] = $item;
        }

        return $result;
    }

    /**
     * Menu item getter.
     *
     * @param \Cake\Datasource\EntityInterface $menu Menu entity
     * @param array $item Menu item
     * @param int $order Menu item order
     * @return array
     */
    protected function _getMenuItem(EntityInterface $menu, array $item, $order = 0)
    {
        if (empty($item)) {
            return [];
        }

        $label = $item['label'];
        $icon = $item['icon'];
        $type = $item['type'];

        $children = !empty($item['children']) ? $item['children'] : [];

        if (!empty($children)) {
            $count = 0;
            foreach ($children as $key => $child) {
                $children[$key] = $this->_getMenuItem($menu, $child, ++$count);
            }
        }

        if (static::TYPE_MODULE === $type) {
            $item = $this->_getMenuItemsFromEvent($menu, [$item['url']]);
            reset($item);
            $item = current($item);
        }

        if (!empty($children)) {
            $item['children'] = $children;
        }

        if (empty($item)) {
            return [];
        }

        if (static::TYPE_MODULE === $type) {
            $item['label'] = $label;
            $item['icon'] = $icon;
        }

{'fraction_non_alphanumeric': 0.15380961428842865, 'fraction_numerical': 0.0009500285008550256, 'mean_word_length': 1.95453269716531, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 103, 'https://': 6, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1045582', 'n_tokens_mistral': 3120, 'n_tokens_neox': 3038, 'n_words': 709}