Completed
Push — develop ( 0a51be...be5f7a )
by Seth
02:20
created
Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
require_once 'common.inc.php';
4
5
use smtech\GradingAnalytics\Toolbox;
6
use smtech\ReflexiveCanvasLTI\LTI\ToolProvider;
7
use smtech\ReflexiveCanvasLTI\Exception\ConfigurationException;
8
9
$ACTION_CONFIG ='config';
10
$ACTION_INSTALL = 'install';
11
$ACTION_CONSUMERS = 'consumers';
12
$ACTION_UNSPECIFIED = false;
13
14
/* store any requested actions for future handling */
15
$action = (empty($_REQUEST['action']) ?
16
    $ACTION_UNSPECIFIED :
17
    strtolower($_REQUEST['action'])
18
);
19
20
/* action requests only come from outside the LTI! */
21
if ($action) {
22
    unset($_SESSION[ToolProvider::class]);
23
}
24
25
/* authenticate LTI launch request, if present */
26
if ($toolbox->lti_isLaunching()) {
27
    $toolbox->resetSession();
28
    $toolbox->lti_authenticate();
29
    exit;
30
}
31
32
/* if authenticated LTI launch, redirect to appropriate placement view */
33
if (!empty($_SESSION[ToolProvider::class]['canvas']['account_id'])) {
34
    $_SESSION[ACCOUNT_ID] = $_SESSION[ToolProvider::class]['canvas']['account_id'];
35
    header("Location: account/index.php");
36
    exit;
37
} elseif (!empty($_SESSION[ToolProvider::class]['canvas']['course_id'])) {
38
    $_SESSION[COURSE_ID] = $_SESSION[ToolProvider::class]['canvas']['course_id'];
39
    header('Location: course/index.php');
40
    exit;
41
42
/* if not authenticated, default to showing credentials */
43
} else {
44
    $action = (empty($action) ?
45
        $ACTION_CONFIG :
46
        $action
47
    );
48
}
49
50
/* process any actions */
51
switch ($action) {
52
    /* reset cached install data from config file */
53
    case $ACTION_INSTALL:
54
        $_SESSION['toolbox'] = Toolbox::fromConfiguration(CONFIG_FILE, true);
55
        $toolbox =& $_SESSION['toolbox'];
56
57
        /* test to see if we can connect to the API */
58
        try {
59
            $toolbox->getAPI();
60
        } catch (ConfigurationException $e) {
61
            /* if there isn't an API token in config.xml, are there OAuth credentials? */
62
            if ($e->getCode() === ConfigurationException::CANVAS_API_INCORRECT) {
63
                $toolbox->interactiveGetAccessToken(
0 ignored issues
show
It seems like you code against a specific sub-type and not the parent class smtech\ReflexiveCanvasLTI\Toolbox as the method interactiveGetAccessToken() does only exist in the following sub-classes of smtech\ReflexiveCanvasLTI\Toolbox: smtech\GradingAnalytics\Toolbox, smtech\StMarksReflexiveCanvasLTI\Toolbox. 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...
64
                    'This tool requires access to the Canvas APIs by an administrative user. ' .
65
                    'This API access is used to query student analytics data that is presented on ' .
66
                    'the Advisor Dashboard. Please enter the URL of your Canvas instance below ' .
67
                    '(e.g. <code>https://canvas.instructure.com</code> -- the URL that you would ' .
68
                    'enter to log in to Canvas). If you are not already logged in, you will be asked ' .
69
                    'to log in. After logging in, you will be asked to authorize this tool.</p>' .
70
                    '<p>If you are already logged, but <em>not</em> logged in as an administrative user, ' .
71
                    'please log out now, so that you may log in as administrative user to authorize this tool.'
72
                );
73
                exit;
74
            } else { /* no (understandable) API credentials available -- doh! */
75
                throw $e;
76
            }
77
        }
78
79
        /* finish by opening consumers control panel */
80
        header('Location: consumers.php');
81
        exit;
82
83
    /* show LTI configuration XML file */
84
    case $ACTION_CONFIG:
85
        header('Content-type: application/xml');
86
        echo $toolbox->saveConfigurationXML();
87
        exit;
88
}
89