Issues (45)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/Controller/EntityCreateMethods.php (5 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
2
3
/**
4
 * This file is part of slick/mvc package
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Slick\Mvc\Controller;
11
12
use Slick\Common\Log;
13
use Slick\Form\FormInterface;
14
use Slick\Mvc\ControllerInterface;
15
use Slick\Mvc\Exception\Service\InvalidFormDataException;
16
use Slick\Mvc\Form\EntityForm;
17
use Slick\Mvc\Service\Entity\EntityUpdateService;
18
use Slick\Orm\Entity;
19
use Slick\Orm\EntityInterface;
20
21
/**
22
 * Entity Create Methods
23
 * 
24
 * @package Slick\Mvc\Controller
25
 * @author  Filipe Silva <[email protected]>
26
 */
27
trait EntityCreateMethods
28
{
29
30
    /**
31
     * Handle the add entity request
32
     */
33
    public function add()
34
    {
35
        $form = $this->getForm();
36
        $this->set(compact('form'));
37
        
38
        if (!$form->wasSubmitted()) {
39
            return;
40
        }
41
        
42
        try {
43
            $this->getUpdateService()
44
                ->setForm($form)
0 ignored issues
show
$form of type object<Slick\Form\FormInterface> is not a sub-type of object<Slick\Mvc\Form\EntityForm>. It seems like you assume a concrete implementation of the interface Slick\Form\FormInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
45
                ->update();
46
            ;
47
        } catch (InvalidFormDataException $caught) {
48
            Log::logger()->addNotice($caught->getMessage(), $form->getData());
0 ignored issues
show
The method addNotice() does not exist on Psr\Log\LoggerInterface. Did you maybe mean notice()?

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...
49
            $this->addErrorMessage($this->getInvalidFormDataMessage());
50
            return;
51
        } catch (\Exception $caught) {
52
            Log::logger()->addCritical(
0 ignored issues
show
The method addCritical() does not exist on Psr\Log\LoggerInterface. Did you maybe mean critical()?

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...
53
                $caught->getMessage(),
54
                $form->getData()
55
            );
56
            $this->addErrorMessage($this->getGeneralErrorMessage($caught));
57
            return;
58
        }
59
        
60
        $this->addSuccessMessage(
61
            $this->getCreateSuccessMessage(
62
                $this->getUpdateService()->getEntity()
63
            )
64
        );
65
        $this->redirectFromCreated($this->getUpdateService()->getEntity());
66
    }
67
    
68
    /**
69
     * Get the create successful entity message
70
     * 
71
     * @param EntityInterface $entity
72
     * 
73
     * @return string
74
     */
75
    protected function getCreateSuccessMessage(EntityInterface $entity)
76
    {
77
        $singleName = $this->getEntityNameSingular();
78
        $message = "The {$singleName} '%s' was successfully created.";
79
        return sprintf($this->translate($message), $entity);
80
    }
81
82
    /**
83
     * Redirect after successful entity creation
84
     * 
85
     * @param EntityInterface $entity
86
     * 
87
     * @return $this|ControllerInterface|static
88
     */
89
    protected function redirectFromCreated(EntityInterface $entity)
90
    {
91
        return $this->redirect(
92
            $this->getBasePath().'/show/'.$entity->getId()
0 ignored issues
show
It seems like getBasePath() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
93
        );
94
    }
95
96
    /**
97
     * Get update service
98
     *
99
     * @return EntityUpdateService
100
     */
101
    abstract public function getUpdateService();
102
103
    /**
104
     * @return FormInterface|EntityForm
105
     */
106
    abstract function getForm();
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
107
108
    /**
109
     * Get invalid form data message
110
     *
111
     * @param \Exception $caught
112
     *
113
     * @return string
114
     */
115
    abstract protected function getGeneralErrorMessage(\Exception $caught);
116
117
    /**
118
     * Get invalid form data message
119
     *
120
     * @return string
121
     */
122
    abstract protected function getInvalidFormDataMessage();
123
124
    /**
125
     * Sets a value to be used by render
126
     *
127
     * The key argument can be an associative array with values to be set
128
     * or a string naming the passed value. If an array is given then the
129
     * value will be ignored.
130
     *
131
     * Those values must be set in the request attributes so they can be used
132
     * latter by any other middle ware in the stack.
133
     *
134
     * @param string|array $key
135
     * @param mixed        $value
136
     *
137
     * @return ControllerInterface
138
     */
139
    abstract public function set($key, $value = null);
140
141
    /**
142
     * Redirects the flow to another route/path
143
     *
144
     * @param string $path the route or path to redirect to
145
     *
146
     * @return ControllerInterface|self|$this
147
     */
148
    abstract public function redirect($path);
149
150
    /**
151
     * Add an error flash message
152
     *
153
     * @param string $message
154
     * @return self
155
     */
156
    abstract public function addErrorMessage($message);
157
158
    /**
159
     * Add a success flash message
160
     *
161
     * @param string $message
162
     * @return self
163
     */
164
    abstract public function addSuccessMessage($message);
165
166
    /**
167
     * Returns the translation for the provided message
168
     *
169
     * @param string $message
170
     * @param string $domain
171
     * @param string $locale
172
     *
173
     * @return string
174
     */
175
    abstract public function translate(
176
        $message, $domain = null, $locale = null
177
    );
178
179
    /**
180
     * Get entity singular name used on controller actions
181
     *
182
     * @return string
183
     */
184
    abstract protected function getEntityNameSingular();
185
}