PLTwig   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 3
c 2
b 0
f 0
lcom 0
cbo 4
dl 0
loc 27
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A load() 0 19 3
1
<?php
2
3
namespace Hospitalplugin\Twig;
4
5
use Hospitalplugin\Twig\EscapePLCharsExtension;
6
7
class PLTwig {
8
	/**
9
	 * Loads Twig with polish settings.
10
	 * 
11
	 * @param string $viewsDir        	
12
	 * @return void|\Twig_Environment
13
	 */
14
	public static function load($viewsDir) {
15
		if (! class_exists ( 'Twig_Loader_Filesystem' )) {
16
			echo 'Twig not activated. Make sure you activate the plugin in
17
    <a href="/wp-admin/plugins.php#timber">/wp-admin/plugins.php</a>';
18
			return;
19
		}
20
		\Twig_Autoloader::register ();
21
		try {
22
			$loader = new \Twig_Loader_Filesystem ( $viewsDir );
23
			$twig = new \Twig_Environment ( $loader, array () );
24
			// 'debug' => true
25
			// 'cache' => '/tmp/'
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
26
			$twig->getExtension ( 'core' )->setTimezone ( 'Europe/Warsaw' ); //
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Twig_ExtensionInterface as the method setTimezone() does only exist in the following implementations of said interface: Twig_Extension_Core.

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...
27
			$twig->addExtension ( new EscapePLCharsExtension () );
28
			return $twig;
29
		} catch ( Exception $e ) {
0 ignored issues
show
Bug introduced by
The class Hospitalplugin\Twig\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
30
			echo "ERR: " . $e;
31
		}
32
	}
33
}