Completed
Push — master ( 5bd517...c7b86e )
by Tomáš
08:52
created

PostRoute   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 2
dl 0
loc 37
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A matches() 0 4 1
A buildOutputPath() 0 4 1
A buildRelativeUrl() 0 8 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Symplify
7
 * Copyright (c) 2016 Tomas Votruba (http://tomasvotruba.cz).
8
 */
9
10
namespace Symplify\PHP7_Sculpin\Renderable\Routing\Route;
11
12
use Symplify\PHP7_Sculpin\Configuration\Configuration;
13
use Symplify\PHP7_Sculpin\Contract\Renderable\Routing\Route\RouteInterface;
14
use Symplify\PHP7_Sculpin\Renderable\File\AbstractFile;
15
use Symplify\PHP7_Sculpin\Renderable\File\PostFile;
16
use Symplify\PHP7_Sculpin\Utils\PathNormalizer;
17
18
final class PostRoute implements RouteInterface
19
{
20
    /**
21
     * @var Configuration
22
     */
23
    private $configuration;
24
25
    public function __construct(Configuration $configuration)
26
    {
27
        $this->configuration = $configuration;
28
    }
29
30
    public function matches(AbstractFile $file) : bool
31
    {
32
        return $file instanceof PostFile;
33
    }
34
35
    /**
36
     * @param PostFile $file
37
     */
38
    public function buildOutputPath(AbstractFile $file) : string
39
    {
40
        return PathNormalizer::normalize($this->buildRelativeUrl($file) . '/index.html');
41
    }
42
43
    /**
44
     * @param PostFile $file
45
     */
46
    public function buildRelativeUrl(AbstractFile $file) : string
47
    {
48
        $permalink = $this->configuration->getPostRoute();
49
        $permalink = preg_replace('/:year/', $file->getDateInFormat('Y'), $permalink);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symplify\PHP7_Sculpin\Renderable\File\AbstractFile as the method getDateInFormat() does only exist in the following sub-classes of Symplify\PHP7_Sculpin\Renderable\File\AbstractFile: Symplify\PHP7_Sculpin\Renderable\File\PostFile. 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...
50
        $permalink = preg_replace('/:month/', $file->getDateInFormat('m'), $permalink);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symplify\PHP7_Sculpin\Renderable\File\AbstractFile as the method getDateInFormat() does only exist in the following sub-classes of Symplify\PHP7_Sculpin\Renderable\File\AbstractFile: Symplify\PHP7_Sculpin\Renderable\File\PostFile. 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...
51
        $permalink = preg_replace('/:day/', $file->getDateInFormat('d'), $permalink);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symplify\PHP7_Sculpin\Renderable\File\AbstractFile as the method getDateInFormat() does only exist in the following sub-classes of Symplify\PHP7_Sculpin\Renderable\File\AbstractFile: Symplify\PHP7_Sculpin\Renderable\File\PostFile. 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...
52
        return preg_replace('/:title/', $file->getFilenameWithoutDate(), $permalink);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symplify\PHP7_Sculpin\Renderable\File\AbstractFile as the method getFilenameWithoutDate() does only exist in the following sub-classes of Symplify\PHP7_Sculpin\Renderable\File\AbstractFile: Symplify\PHP7_Sculpin\Renderable\File\PostFile. 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...
53
    }
54
}
55