AOD_IndexController::action_indexdata()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
 /**
3
 * 
4
 * 
5
 * @package 
6
 * @copyright SalesAgility Ltd http://www.salesagility.com
7
 * 
8
 * This program is free software; you can redistribute it and/or modify
9
 * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
10
 * the Free Software Foundation; either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
19
 * along with this program; if not, see http://www.gnu.org/licenses
20
 * or write to the Free Software Foundation,Inc., 51 Franklin Street,
21
 * Fifth Floor, Boston, MA 02110-1301  USA
22
 *
23
 * @author Salesagility Ltd <[email protected]>
24
 */
25
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
26
27
class AOD_IndexController extends SugarController {
28
29
    protected $action_remap = array('index'=>'indexdata');
30
31
    function action_indexdata() {
32
        $this->view = 'indexdata';
33
    }
34
    function action_optimise(){
35
        set_time_limit(6000);
36
        $index = BeanFactory::getBean("AOD_Index")->getIndex();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class SugarBean as the method getIndex() does only exist in the following sub-classes of SugarBean: AOD_Index. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
37
        $index->optimise();
38
        SugarApplication::redirect('index.php?module=AOD_Index');
39
    }
40
}