GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

PostManager::createPost()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the CCDNForum ForumBundle
5
 *
6
 * (c) CCDN (c) CodeConsortium <http://www.codeconsortium.com/>
7
 *
8
 * Available on github <http://www.github.com/codeconsortium/>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace CCDNForum\ForumBundle\Model\Component\Manager;
15
16
use Symfony\Component\Security\Core\User\UserInterface;
17
18
use CCDNForum\ForumBundle\Model\Component\Gateway\GatewayInterface;
19
use CCDNForum\ForumBundle\Model\Component\Manager\ManagerInterface;
20
use CCDNForum\ForumBundle\Model\Component\Manager\BaseManager;
21
use CCDNForum\ForumBundle\Component\Helper\PostLockHelper;
22
23
use CCDNForum\ForumBundle\Entity\Post;
24
25
/**
26
 *
27
 * @category CCDNForum
28
 * @package  ForumBundle
29
 *
30
 * @author   Reece Fowell <[email protected]>
31
 * @license  http://opensource.org/licenses/MIT MIT
32
 * @version  Release: 2.0
33
 * @link     https://github.com/codeconsortium/CCDNForumForumBundle
34
 *
35
 */
36
class PostManager extends BaseManager implements ManagerInterface
37
{
38
    /**
39
     *
40
     * @access protected
41
     * @var \CCDNForum\ForumBundle\Component\Helper\PostLockHelper $postLockHelper
42
     */
43
    protected $postLockHelper;
44
45
    /**
46
     *
47
     * @access public
48
     * @param \CCDNForum\ForumBundle\Gateway\GatewayInterface        $gateway
0 ignored issues
show
Documentation introduced by
Should the type for parameter $gateway not be GatewayInterface?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
49
     * @param \CCDNForum\ForumBundle\Component\Helper\PostLockHelper $postLockHelper
50
     */
51
    public function __construct(GatewayInterface $gateway, PostLockHelper $postLockHelper)
52
    {
53
        $this->gateway = $gateway;
54
        $this->postLockHelper = $postLockHelper;
55
    }
56
57
    /**
58
     *
59
     * @access public
60
     * @return \CCDNForum\ForumBundle\Entity\Post
61
     */
62
    public function createPost()
63
    {
64
        return $this->gateway->createPost();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface CCDNForum\ForumBundle\Mo...ateway\GatewayInterface as the method createPost() does only exist in the following implementations of said interface: CCDNForum\ForumBundle\Mo...ent\Gateway\PostGateway.

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...
65
    }
66
67
    /**
68
     *
69
     * @access public
70
     * @param  \CCDNForum\ForumBundle\Entity\Post              $post
71
     * @return \CCDNForum\ForumBundle\Manager\ManagerInterface
0 ignored issues
show
Documentation introduced by
Should the return type not be PostManager?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
72
     */
73
    public function savePost(Post $post)
74
    {
75
        $this->postLockHelper->setLockLimitOnPost($post);
76
77
        $this->gateway->savePost($post);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface CCDNForum\ForumBundle\Mo...ateway\GatewayInterface as the method savePost() does only exist in the following implementations of said interface: CCDNForum\ForumBundle\Mo...ent\Gateway\PostGateway.

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...
78
79
        // refresh the user so that we have an PostId to work with.
80
        $this->refresh($post);
81
82
        return $this;
83
    }
84
85
    /**
86
     *
87
     * @access public
88
     * @param  \CCDNForum\ForumBundle\Entity\Post              $post
89
     * @return \CCDNForum\ForumBundle\Manager\ManagerInterface
0 ignored issues
show
Documentation introduced by
Should the return type not be PostManager?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
90
     */
91
    public function updatePost(Post $post)
92
    {
93
        $this->gateway->updatePost($post);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface CCDNForum\ForumBundle\Mo...ateway\GatewayInterface as the method updatePost() does only exist in the following implementations of said interface: CCDNForum\ForumBundle\Mo...ent\Gateway\PostGateway.

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...
94
95
        return $this;
96
    }
97
98
    /**
99
     *
100
     * @access public
101
     * @param  \CCDNForum\ForumBundle\Entity\Post              $post
102
     * @return \CCDNForum\ForumBundle\Manager\ManagerInterface
0 ignored issues
show
Documentation introduced by
Should the return type not be PostManager?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
103
     */
104
    public function lock(Post $post)
105
    {
106
        $post->setUnlockedUntilDate(new \Datetime('now'));
107
        $this->persist($post)->flush();
108
        $this->refresh($post);
109
110
        return $this;
111
    }
112
113
    /**
114
     *
115
     * @access public
116
     * @param  \CCDNForum\ForumBundle\Entity\Post              $post
117
     * @return \CCDNForum\ForumBundle\Manager\ManagerInterface
0 ignored issues
show
Documentation introduced by
Should the return type not be PostManager?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
118
     */
119
    public function restore(Post $post)
120
    {
121
        $post->setDeleted(false);
122
        $post->setDeletedBy(null);
123
        $post->setDeletedDate(null);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a object<DateTime>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
124
125
        // update the record
126
        $this->persist($post)->flush();
127
128
        if ($post->getTopic()) {
129
            $topic = $post->getTopic();
130
131
            // if this is the first post and only post,
132
            // then restore the topic aswell.
133
            if ($topic->getCachedReplyCount() < 1) {
134
                $topic->setDeleted(false);
0 ignored issues
show
Bug introduced by
The method setDeleted() does not exist on CCDNForum\ForumBundle\Entity\Model\Topic. Did you maybe mean setDeletedBy()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
135
                $topic->setDeletedBy(null);
136
                $topic->setDeletedDate(null);
137
138
                $this->persist($topic)->flush();
139
            }
140
        }
141
142
        return $this;
143
    }
144
145
    /**
146
     *
147
     * @access public
148
     * @param  \CCDNForum\ForumBundle\Entity\Post                  $post
149
     * @param  \Symfony\Component\Security\Core\User\UserInterface $user
150
     * @return \CCDNForum\ForumBundle\Manager\ManagerInterface
0 ignored issues
show
Documentation introduced by
Should the return type not be PostManager?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
151
     */
152
    public function softDelete(Post $post, UserInterface $user)
153
    {
154
        // Don't overwite previous users accountability.
155
        if (! $post->getDeletedBy() && ! $post->getDeletedDate()) {
156
            $post->setDeleted(true);
157
            $post->setDeletedBy($user);
158
            $post->setDeletedDate(new \DateTime());
159
160
            // update the record
161
            $this->persist($post)->flush();
162
        }
163
164
        return $this;
165
    }
166
}
167