__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 5
cp 0
crap 2
1
<?php
2
class Intraface_Controller_ModuleGatekeeper extends k_Component
3
{
4
    protected $template;
5
    protected $mdb2;
6
7
    function __construct(k_TemplateFactory $template, MDB2_Driver_Common $mdb2)
8
    {
9
        $this->template = $template;
10
        $this->mdb2 = $mdb2;
11
    }
12
13
    function map($name)
14
    {
15
        foreach ($this->getModules() as $module) {
16
            if ($module['name'] == $name) {
17
                try {
18
                    $this->getKernel()->module($name);
19
                } catch (Exception $e) {
20
                    throw new Exception('No access to module ' . $name . ': ' . $e->getMessage());
21
                }
22
                return 'Intraface_modules_'.$name.'_Controller_Index';
23
            }
24
        }
25
        return parent::map($name);
26
    }
27
28
    function renderHtml()
29
    {
30
        $this->document->setTitle('Module gatekeeper');
31
32
        require_once 'Intraface/modules/intranetmaintenance/ModuleMaintenance.php';
33
34
        $smarty = $this->template->create(dirname(__FILE__) . '/templates/modulegatekeeper');
35
        return $smarty->render($this);
36
    }
37
38
    public function getKernel()
39
    {
40
        return $this->context->getKernel();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface k_Context as the method getKernel() does only exist in the following implementations of said interface: Intraface_Controller_Index, Intraface_Controller_ModuleGatekeeper, Intraface_Controller_ModulePackage_Process, Intraface_Controller_Restricted, Intraface_Controller_SwitchIntranet, Intraface_Filehandler_Controller_Batchedit, Intraface_Filehandler_Controller_CKEditor, Intraface_Filehandler_Controller_Crop, Intraface_Filehandler_Controller_Index, Intraface_Filehandler_Controller_SelectFile, Intraface_Filehandler_Controller_Show, Intraface_Filehandler_Controller_Size, Intraface_Filehandler_Controller_Sizes, Intraface_Filehandler_Controller_Upload, Intraface_Filehandler_Controller_UploadMultiple, Intraface_Filehandler_Controller_UploadScript, Intraface_Fileimport_Controller_Index, Intraface_Keyword_Controller_Connect, Intraface_Keyword_Controller_Index, Intraface_Keyword_Controller_Show, Intraface_modules_accoun...ontroller_Account_Index, Intraface_modules_accoun...ontroller_Account_Popup, Intraface_modules_accoun...Controller_Account_Show, Intraface_modules_accounting_Controller_Daybook, Intraface_modules_accounting_Controller_Index, Intraface_modules_accounting_Controller_Post_Index, Intraface_modules_accounting_Controller_Post_Show, Intraface_modules_accounting_Controller_Search, Intraface_modules_accounting_Controller_Settings, Intraface_modules_accounting_Controller_State, Intraface_modules_accoun...roller_State_Creditnote, Intraface_modules_accoun...ller_State_Depreciation, Intraface_modules_accoun...ontroller_State_Invoice, Intraface_modules_accoun...ontroller_State_Payment, Intraface_modules_accoun...oller_State_Procurement, Intraface_modules_accoun...ntroller_State_Reminder, Intraface_modules_accoun...roller_State_SelectYear, Intraface_modules_accounting_Controller_Vat_Index, Intraface_modules_accounting_Controller_Vat_Show, Intraface_modules_accoun...ontroller_Voucher_Index, Intraface_modules_accoun...Controller_Voucher_Show, Intraface_modules_accounting_Controller_Year_Edit, Intraface_modules_accounting_Controller_Year_End, Intraface_modules_accounting_Controller_Year_Index, Intraface_modules_accoun...troller_Year_Primosaldo, Intraface_modules_accounting_Controller_Year_Show, Intraface_modules_administration_Controller_Index, Intraface_modules_admini...ion_Controller_Intranet, Intraface_modules_cms_Controller_Element, Intraface_modules_cms_Controller_Elements, Intraface_modules_cms_Controller_Index, Intraface_modules_cms_Controller_Navigation, Intraface_modules_cms_Controller_Page, Intraface_modules_cms_Controller_Pages, Intraface_modules_cms_Controller_Section, Intraface_modules_cms_Controller_Sections, Intraface_modules_cms_Controller_Site, Intraface_modules_cms_Controller_Stylesheet, Intraface_modules_cms_Controller_Template, Intraface_modules_cms_Co...ler_TemplateSectionEdit, Intraface_modules_cms_Controller_TemplateSections, Intraface_modules_cms_Controller_Templates, Intraface_modules_contac...troller_BatchNewsletter, Intraface_modules_contact_Controller_Choosecontact, Intraface_modules_contac...ntroller_Contactpersons, Intraface_modules_contact_Controller_Import, Intraface_modules_contact_Controller_Index, Intraface_modules_contact_Controller_Memos, Intraface_modules_contact_Controller_Merge, Intraface_modules_contact_Controller_Sendemail, Intraface_modules_contact_Controller_Show, Intraface_modules_contro...ntroller_ChangePassword, Intraface_modules_controlpanel_Controller_Index, Intraface_modules_controlpanel_Controller_User, Intraface_modules_contro...troller_UserPreferences, Intraface_modules_curren...ller_ExchangeRate_Index, Intraface_modules_curren...changeRate_ProductPrice, Intraface_modules_currency_Controller_Index, Intraface_modules_currency_Controller_Show, Intraface_modules_debtor_Controller_Collection, Intraface_modules_debtor_Controller_Create, Intraface_modules_debtor_Controller_Depreciation, Intraface_modules_debtor_Controller_Depreciations, Intraface_modules_debtor_Controller_Index, Intraface_modules_debtor_Controller_Item, Intraface_modules_debtor_Controller_Items, Intraface_modules_debtor_Controller_Payment, Intraface_modules_debtor_Controller_Payments, Intraface_modules_debtor_Controller_Reminder, Intraface_modules_debtor_Controller_Reminders, Intraface_modules_debtor_Controller_Settings, Intraface_modules_debtor_Controller_Show, Intraface_modules_debtor_Controller_Typenegotiator, Intraface_modules_email_Controller_Email, Intraface_modules_email_Controller_Index, Intraface_modules_fileimport_Controller_Index, Intraface_modules_filemanager_Controller_Index, Intraface_modules_intran...enance_Controller_Index, Intraface_modules_intran...ntroller_Intranet_Index, Intraface_modules_intran...Controller_Intranet_Key, Intraface_modules_intran...ler_Intranet_Permission, Intraface_modules_intran...ontroller_Intranet_Show, Intraface_modules_intran...ance_Controller_Modules, Intraface_modules_intran...e_Controller_User_Index, Intraface_modules_intran...troller_User_Permission, Intraface_modules_intran...ce_Controller_User_Show, Intraface_modules_module...e_Controller_AddPackage, Intraface_modules_modulepackage_Controller_Index, Intraface_modules_modulepackage_Controller_Package, Intraface_modules_modulepackage_Controller_Payment, Intraface_modules_module...age_Controller_PostForm, Intraface_modules_modulepackage_Controller_Process, Intraface_modules_newsletter_Controller_Index, Intraface_modules_newsletter_Controller_Letter, Intraface_modules_newsletter_Controller_Letters, Intraface_modules_newsletter_Controller_List, Intraface_modules_newsletter_Controller_Lists, Intraface_modules_newsletter_Controller_Log, Intraface_modules_newsletter_Controller_Send, Intraface_modules_newsle..._Controller_Subscribers, Intraface_modules_onlinepayment_Controller_Index, Intraface_modules_online...ent_Controller_Settings, Intraface_modules_payment_Controller_Index, Intraface_modules_payment_Controller_Show, Intraface_modules_procurement_Controller_Index, Intraface_modules_procurement_Controller_Item, Intraface_modules_procurement_Controller_Items, Intraface_modules_procur...ontroller_PurchasePrice, Intraface_modules_procurement_Controller_Show, Intraface_modules_produc...troller_AttributeGroups, Intraface_modules_produc...tributeGroups_Attribute, Intraface_modules_produc...er_AttributeGroups_Show, Intraface_modules_product_Controller_BatchEdit, Intraface_modules_produc...oller_BatchPriceChanger, Intraface_modules_product_Controller_Index, Intraface_modules_produc...r_Productattributegroup, Intraface_modules_product_Controller_Related, Intraface_modules_product_Controller_Selectproduct, Intraface_modules_produc..._Selectproductvariation, Intraface_modules_product_Controller_Show, Intraface_modules_produc...ntroller_Show_PlainText, Intraface_modules_produc...troller_Show_Variations, Intraface_modules_produc...s_SelectAttributeGroups, Intraface_modules_product_Controller_Variation, Intraface_modules_shop_C...r_BasketEvaluation_Edit, Intraface_modules_shop_C..._BasketEvaluation_Index, Intraface_modules_shop_C...r_BasketEvaluation_Show, Intraface_modules_shop_Controller_Categories, Intraface_modules_shop_C...oller_Categories_Create, Intraface_modules_shop_Controller_Categories_Show, Intraface_modules_shop_C...oller_DiscountCampaigns, Intraface_modules_shop_Controller_FeaturedProducts, Intraface_modules_shop_Controller_Index, Intraface_modules_shop_Controller_Show, Intraface_modules_stock_Controller_Index, Intraface_modules_stock_Controller_Product, Intraface_modules_stock_Controller_Variations, Intraface_modules_todo_Controller_Edit, Intraface_modules_todo_Controller_Index, Intraface_modules_todo_Controller_Todo.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
41
    }
42
43
    function getUser()
44
    {
45
        return $this->context->getKernel()->user;
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface k_Context as the method getKernel() does only exist in the following implementations of said interface: Intraface_Controller_Index, Intraface_Controller_ModuleGatekeeper, Intraface_Controller_ModulePackage_Process, Intraface_Controller_Restricted, Intraface_Controller_SwitchIntranet, Intraface_Filehandler_Controller_Batchedit, Intraface_Filehandler_Controller_CKEditor, Intraface_Filehandler_Controller_Crop, Intraface_Filehandler_Controller_Index, Intraface_Filehandler_Controller_SelectFile, Intraface_Filehandler_Controller_Show, Intraface_Filehandler_Controller_Size, Intraface_Filehandler_Controller_Sizes, Intraface_Filehandler_Controller_Upload, Intraface_Filehandler_Controller_UploadMultiple, Intraface_Filehandler_Controller_UploadScript, Intraface_Fileimport_Controller_Index, Intraface_Keyword_Controller_Connect, Intraface_Keyword_Controller_Index, Intraface_Keyword_Controller_Show, Intraface_modules_accoun...ontroller_Account_Index, Intraface_modules_accoun...ontroller_Account_Popup, Intraface_modules_accoun...Controller_Account_Show, Intraface_modules_accounting_Controller_Daybook, Intraface_modules_accounting_Controller_Index, Intraface_modules_accounting_Controller_Post_Index, Intraface_modules_accounting_Controller_Post_Show, Intraface_modules_accounting_Controller_Search, Intraface_modules_accounting_Controller_Settings, Intraface_modules_accounting_Controller_State, Intraface_modules_accoun...roller_State_Creditnote, Intraface_modules_accoun...ller_State_Depreciation, Intraface_modules_accoun...ontroller_State_Invoice, Intraface_modules_accoun...ontroller_State_Payment, Intraface_modules_accoun...oller_State_Procurement, Intraface_modules_accoun...ntroller_State_Reminder, Intraface_modules_accoun...roller_State_SelectYear, Intraface_modules_accounting_Controller_Vat_Index, Intraface_modules_accounting_Controller_Vat_Show, Intraface_modules_accoun...ontroller_Voucher_Index, Intraface_modules_accoun...Controller_Voucher_Show, Intraface_modules_accounting_Controller_Year_Edit, Intraface_modules_accounting_Controller_Year_End, Intraface_modules_accounting_Controller_Year_Index, Intraface_modules_accoun...troller_Year_Primosaldo, Intraface_modules_accounting_Controller_Year_Show, Intraface_modules_administration_Controller_Index, Intraface_modules_admini...ion_Controller_Intranet, Intraface_modules_cms_Controller_Element, Intraface_modules_cms_Controller_Elements, Intraface_modules_cms_Controller_Index, Intraface_modules_cms_Controller_Navigation, Intraface_modules_cms_Controller_Page, Intraface_modules_cms_Controller_Pages, Intraface_modules_cms_Controller_Section, Intraface_modules_cms_Controller_Sections, Intraface_modules_cms_Controller_Site, Intraface_modules_cms_Controller_Stylesheet, Intraface_modules_cms_Controller_Template, Intraface_modules_cms_Co...ler_TemplateSectionEdit, Intraface_modules_cms_Controller_TemplateSections, Intraface_modules_cms_Controller_Templates, Intraface_modules_contac...troller_BatchNewsletter, Intraface_modules_contact_Controller_Choosecontact, Intraface_modules_contac...ntroller_Contactpersons, Intraface_modules_contact_Controller_Import, Intraface_modules_contact_Controller_Index, Intraface_modules_contact_Controller_Memos, Intraface_modules_contact_Controller_Merge, Intraface_modules_contact_Controller_Sendemail, Intraface_modules_contact_Controller_Show, Intraface_modules_contro...ntroller_ChangePassword, Intraface_modules_controlpanel_Controller_Index, Intraface_modules_controlpanel_Controller_User, Intraface_modules_contro...troller_UserPreferences, Intraface_modules_curren...ller_ExchangeRate_Index, Intraface_modules_curren...changeRate_ProductPrice, Intraface_modules_currency_Controller_Index, Intraface_modules_currency_Controller_Show, Intraface_modules_debtor_Controller_Collection, Intraface_modules_debtor_Controller_Create, Intraface_modules_debtor_Controller_Depreciation, Intraface_modules_debtor_Controller_Depreciations, Intraface_modules_debtor_Controller_Index, Intraface_modules_debtor_Controller_Item, Intraface_modules_debtor_Controller_Items, Intraface_modules_debtor_Controller_Payment, Intraface_modules_debtor_Controller_Payments, Intraface_modules_debtor_Controller_Reminder, Intraface_modules_debtor_Controller_Reminders, Intraface_modules_debtor_Controller_Settings, Intraface_modules_debtor_Controller_Show, Intraface_modules_debtor_Controller_Typenegotiator, Intraface_modules_email_Controller_Email, Intraface_modules_email_Controller_Index, Intraface_modules_fileimport_Controller_Index, Intraface_modules_filemanager_Controller_Index, Intraface_modules_intran...enance_Controller_Index, Intraface_modules_intran...ntroller_Intranet_Index, Intraface_modules_intran...Controller_Intranet_Key, Intraface_modules_intran...ler_Intranet_Permission, Intraface_modules_intran...ontroller_Intranet_Show, Intraface_modules_intran...ance_Controller_Modules, Intraface_modules_intran...e_Controller_User_Index, Intraface_modules_intran...troller_User_Permission, Intraface_modules_intran...ce_Controller_User_Show, Intraface_modules_module...e_Controller_AddPackage, Intraface_modules_modulepackage_Controller_Index, Intraface_modules_modulepackage_Controller_Package, Intraface_modules_modulepackage_Controller_Payment, Intraface_modules_module...age_Controller_PostForm, Intraface_modules_modulepackage_Controller_Process, Intraface_modules_newsletter_Controller_Index, Intraface_modules_newsletter_Controller_Letter, Intraface_modules_newsletter_Controller_Letters, Intraface_modules_newsletter_Controller_List, Intraface_modules_newsletter_Controller_Lists, Intraface_modules_newsletter_Controller_Log, Intraface_modules_newsletter_Controller_Send, Intraface_modules_newsle..._Controller_Subscribers, Intraface_modules_onlinepayment_Controller_Index, Intraface_modules_online...ent_Controller_Settings, Intraface_modules_payment_Controller_Index, Intraface_modules_payment_Controller_Show, Intraface_modules_procurement_Controller_Index, Intraface_modules_procurement_Controller_Item, Intraface_modules_procurement_Controller_Items, Intraface_modules_procur...ontroller_PurchasePrice, Intraface_modules_procurement_Controller_Show, Intraface_modules_produc...troller_AttributeGroups, Intraface_modules_produc...tributeGroups_Attribute, Intraface_modules_produc...er_AttributeGroups_Show, Intraface_modules_product_Controller_BatchEdit, Intraface_modules_produc...oller_BatchPriceChanger, Intraface_modules_product_Controller_Index, Intraface_modules_produc...r_Productattributegroup, Intraface_modules_product_Controller_Related, Intraface_modules_product_Controller_Selectproduct, Intraface_modules_produc..._Selectproductvariation, Intraface_modules_product_Controller_Show, Intraface_modules_produc...ntroller_Show_PlainText, Intraface_modules_produc...troller_Show_Variations, Intraface_modules_produc...s_SelectAttributeGroups, Intraface_modules_product_Controller_Variation, Intraface_modules_shop_C...r_BasketEvaluation_Edit, Intraface_modules_shop_C..._BasketEvaluation_Index, Intraface_modules_shop_C...r_BasketEvaluation_Show, Intraface_modules_shop_Controller_Categories, Intraface_modules_shop_C...oller_Categories_Create, Intraface_modules_shop_Controller_Categories_Show, Intraface_modules_shop_C...oller_DiscountCampaigns, Intraface_modules_shop_Controller_FeaturedProducts, Intraface_modules_shop_Controller_Index, Intraface_modules_shop_Controller_Show, Intraface_modules_stock_Controller_Index, Intraface_modules_stock_Controller_Product, Intraface_modules_stock_Controller_Variations, Intraface_modules_todo_Controller_Edit, Intraface_modules_todo_Controller_Index, Intraface_modules_todo_Controller_Todo.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
46
    }
47
48
    function getModules()
49
    {
50
        require_once 'Intraface/modules/intranetmaintenance/ModuleMaintenance.php';
51
52
        $gateway = new Intraface_ModuleGateway($this->mdb2);
53
        return $gateway->getList();
54
    }
55
}
56