Completed
Push — master ( d208aa...25518b )
by Lars
12:37
created

Primosaldo::renderHtmlEdit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 15
nc 1
nop 0
dl 0
loc 23
ccs 0
cts 18
cp 0
crap 2
rs 9.0856
c 0
b 0
f 0
1
<?php
2
class Intraface_modules_accounting_Controller_Year_Primosaldo extends k_Component
3
{
4
    protected $template;
5
6
    function __construct(k_TemplateFactory $template)
7
    {
8
        $this->template = $template;
9
    }
10
11
    function renderHtml()
12
    {
13
        $year = $this->getYear();
14
        $account = new Account($year);
15
16
        $accounts = $account->getList('balance');
17
18
        $total_debet = 0;
19
        $total_credit = 0;
20
21
        $data = array('total_debet' => $total_debet, 'total_credit' => $total_credit, 'account' => $account, 'year' => $year, 'accounts' => $accounts);
22
23
        $smarty = $this->template->create(dirname(__FILE__) . '/../templates/year/primosaldo');
24
        return $smarty->render($this, $data);
25
    }
26
27
    function postForm()
28
    {
29
        $year = $this->getYear();
30
31
        if ($year->get('last_year_id') == 0) {
32
            throw new Exception('No last year set');
33
        }
34
35
        $last_year = new Year($this->getKernel(), $year->get('last_year_id'));
36
37
        // hente konti hvor de nye har created_from_id
38
        $account = new Account($year);
39
        $accounts = $account->getList('balance');
40
        foreach ($accounts as $a) {
41
            //  @todo Should test whether all posts in the past year has been stated
42
            $old_account = new Account($last_year, $a['created_from_id']);
43
            $saldo = $old_account->getSaldo('stated');
44
45
            if ($old_account->get('credit') == $old_account->get('debet')) {
46
                $saldo = array(
47
                    'credit' => 0,
48
                    'debet' => 0
49
                );
50
            } elseif ($old_account->get('credit') > $old_account->get('debet')) {
51
                $saldo = array(
52
                    'credit' => $old_account->get('credit') - $old_account->get('debet'),
53
                    'debet' => 0
54
                );
55
            } elseif ($old_account->get('credit') < $old_account->get('debet')) {
56
                $saldo = array(
57
                    'credit' => 0,
58
                    'debet' => $old_account->get('debet') - $old_account->get('credit')
59
                );
60
            }
61
62
            $account = new Account($year, $a['id']);
63
            $account->savePrimosaldo(number_format($saldo['debet'], 2, ',', ''), number_format($saldo['credit'], 2, ',', ''));
64
        }
65
        return $this->render();
66
    }
67
68
    function renderHtmlEdit()
69
    {
70
        $this->getKernel()->module('accounting');
71
72
        $year = new Year($this->getKernel(), $this->context->name());
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 name() does only exist in the following implementations of said interface: Demo_CMS_Root, Demo_CMS_Show, Demo_Identifier, Demo_Login_Root, Demo_Newsletter_Root, Demo_Newsletter_Show, Demo_Root, Demo_Shop_Root, Demo_Shop_Show, DojoController, HelloComponent, HelloController, Intraface_Controller_Index, Intraface_Controller_Login, Intraface_Controller_Logout, Intraface_Controller_ModuleGatekeeper, Intraface_Controller_ModulePackage_Process, Intraface_Controller_Payment, Intraface_Controller_Restricted, Intraface_Controller_RetrievePassword, Intraface_Controller_Signup, Intraface_Controller_SwitchIntranet, Intraface_Controller_TestLogin, 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_Filehandler_Controller_Viewer, Intraface_Fileimport_Controller_Index, Intraface_Keyword_Controller_Connect, Intraface_Keyword_Controller_Index, Intraface_Keyword_Controller_Show, Intraface_Tools_Controller_Log, Intraface_Tools_Controller_Phpinfo, Intraface_Tools_Controller_Root, Intraface_XMLRPC_Admin_Controller, Intraface_XMLRPC_CMS_Controller, Intraface_XMLRPC_Contact_Controller, Intraface_XMLRPC_Controller, Intraface_XMLRPC_Controller_Server, Intraface_XMLRPC_Debtor_Controller, Intraface_XMLRPC_Newsletter_Controller, Intraface_XMLRPC_OnlinePayment_Controller, Intraface_XMLRPC_Shop_Controller, 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_backup_Controller_Index, 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_contact_Controller_Contactperson, Intraface_modules_contac...ntroller_Contactpersons, Intraface_modules_contact_Controller_Import, Intraface_modules_contact_Controller_Index, Intraface_modules_contact_Controller_Memo, 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_currency_Controller_Add, Intraface_modules_curren...ller_ExchangeRate_Index, Intraface_modules_curren...er_ExchangeRate_Payment, Intraface_modules_curren...changeRate_ProductPrice, Intraface_modules_curren...ler_ExchangeRate_Update, 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_ReminderEmail, Intraface_modules_debtor_Controller_Reminders, Intraface_modules_debtor_Controller_Send, 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_email_Controller_Settings, 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_invoice_Controller_Index, Intraface_modules_language_Controller_Index, 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_newsletter_Controller_Subscriber, Intraface_modules_newsle..._Controller_Subscribers, Intraface_modules_online...ntroller_ChooseProvider, Intraface_modules_onlinepayment_Controller_Index, Intraface_modules_onlinepayment_Controller_Payment, Intraface_modules_online...ent_Controller_Settings, Intraface_modules_order_Controller_Index, 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_quotation_Controller_Index, 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_C...er_PaymentMethods_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_Email, Intraface_modules_todo_Controller_Index, Intraface_modules_todo_Controller_Setting, Intraface_modules_todo_Controller_Todo, Intraface_modules_webshop_Controller_Index, Login, Logout, MyMultiComponent, NotAuthorizedComponent, NotAuthorizedComponent, RegistrationForm, RestrictedController, Root, Template, Thanks, ZfPaginationPage, ZfRegistrationForm, ZfThanks, components_Root, k_Component, k_DefaultForbiddenComponent, k_DefaultMethodNotAllowedComponent, k_DefaultNotAuthorizedComponent, k_DefaultNotImplementedComponent, k_DefaultNotNotAcceptableComponent, k_DefaultPageNotFoundComponent, test_BasicComponent, test_CircularComponent, test_ContentTypeComponent, test_DependingComponent, test_ExposedComponent, test_LoginForm, test_StatefulPage, test_response_HelloAgainComponent, test_response_HelloComponent, test_response_JsonComponent, test_response_RootComponent, test_response_RootThreeComponent, test_response_RootTwoComponent.

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...
73
74
        $account = new Account($year);
75
        $accounts = $account->getList('balance');
76
77
        $total_debet = 0;
0 ignored issues
show
Unused Code introduced by
$total_debet is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
78
        $total_credit = 0;
0 ignored issues
show
Unused Code introduced by
$total_credit is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
79
80
        $data = array(
81
            'total_debet' => 0,
82
            'total_credit' => 0,
83
            'accounts' => $accounts,
84
            'year' => $year,
85
            'account' => $account
86
        );
87
88
        $smarty = $this->template->create(dirname(__FILE__) . '/../templates/year/primosaldo-edit');
89
        return $smarty->render($this, $data);
90
    }
91
92
    function putForm()
0 ignored issues
show
Coding Style introduced by
putForm uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
93
    {
94
        $year = $this->getYear();
95
        foreach ($_POST['id'] as $key => $values) {
96
            $account = new Account($year, $_POST['id'][$key]);
97
            $account->savePrimosaldo($_POST['debet'][$key], $_POST['credit'][$key]);
98
        }
99
        if (!$account->error->isError()) {
0 ignored issues
show
Bug introduced by
The variable $account does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
100
            return new k_SeeOther($this->url());
101
        }
102
        return $this->render();
103
    }
104
105
    function getYear()
106
    {
107
        return $this->context->getYear();
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 getYear() does only exist in the following implementations of said interface: 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_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.

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...
108
    }
109
110
    function getKernel()
111
    {
112
        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...
113
    }
114
}
115