Completed
Push — master ( 1a78df...e4a2dc )
by Seth
01:53
created

common.inc.php (4 issues)

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
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 9 and the first side effect is on line 3.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
require_once __DIR__ . '/vendor/autoload.php';
4
5
use smtech\SeeAllSubmissions\Toolbox;
6
use smtech\ReflexiveCanvasLTI\LTI\ToolProvider;
7
use Battis\DataUtilities;
8
9
define('CONFIG_FILE', __DIR__ . '/config.xml');
10
define('CANVAS_INSTANCE_URL', 'canvas_instance_url');
11
define('ACCOUNT_ID', 'account_id');
12
define('COURSE_ID', 'course_id');
13
14
@session_start(); // TODO I don't feel good about suppressing warnings
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
15
16
/* prepare the toolbox */
17
if (empty($_SESSION[Toolbox::class])) {
18
    $_SESSION[Toolbox::class] =& Toolbox::fromConfiguration(CONFIG_FILE);
19
}
20
$toolbox =& $_SESSION[Toolbox::class];
21
$toolbox->smarty_assign([
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 smarty_assign() does only exist in the following sub-classes of smtech\ReflexiveCanvasLTI\Toolbox: smtech\SeeAllSubmissions\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...
22
    'category' => DataUtilities::titleCase(preg_replace('/[\-_]+/', ' ', basename(__DIR__)))
23
]);
24
$toolbox->getSmarty()->prependTemplateDir(__DIR__ . '/templates');
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 getSmarty() does only exist in the following sub-classes of smtech\ReflexiveCanvasLTI\Toolbox: smtech\SeeAllSubmissions\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...
25
26
/* set the Tool Consumer's instance URL, if present */
27
if (empty($_SESSION[CANVAS_INSTANCE_URL]) &&
28
    !empty($_SESSION[ToolProvider::class]['canvas']['api_domain'])
29
) {
30
    $_SESSION[CANVAS_INSTANCE_URL] = 'https://' . $_SESSION[ToolProvider::class]['canvas']['api_domain'];
31
}
32