Issues (11)

Security Analysis    no request data  

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/Model/BaseModel.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
2
3
/**
4
 * @author   Temitope Olotin <[email protected]>
5
 * @license  <https://opensource.org/license/MIT> MIT
6
 */
7
namespace Laztopaz\PotatoORM;
8
9
class BaseModel implements BaseModelInterface
10
{
11
    // Inject the inflector trait
12
    use Inflector;
13
14
    // Private variable that contains instance of database
15
    protected $databaseModel;
16
17
    // Class variable holding class name pluralized
18
    protected $tableName;
19
20
    // Properties will later contain key, value pairs from the magic setter, getter methods
21
    protected $properties = [];
22
23
    public function __construct()
24
    {
25
        $this->tableName = $this->getClassName();
26
27
        $this->databaseModel = new DatabaseHandler($this->tableName);
28
29
        $this->properties['id'] = 0;
30
    }
31
32
    /**
33
     * The magic getter method.
34
     *
35
     * @params key
36
     *
37
     * @return array key
38
     */
39
    public function __get($key)
40
    {
41
        $this->properties[$key];
42
    }
43
44
    /**
45
     * The magic setter method.
46
     *
47
     * @params property, key
48
     *
49
     * @return array associative array properties
50
     */
51
    public function __set($property, $value)
52
    {
53
        $this->properties[$property] = $value;
54
    }
55
56
    /**
57
     * This method gets all the record from a particular table.
58
     *
59
     * @params void
60
     *
61
     * @throws NoRecordFoundException
62
     *
63
     * @return associative array
64
     */
65
    public static function getAll()
66
    {
67
        $allData = DatabaseHandler::read($id = false, self::getClassName());
68
69
        if (count($allData) > 0) {
70
            return $allData;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $allData; (array) is incompatible with the return type declared by the interface Laztopaz\PotatoORM\BaseModelInterface::getAll of type Laztopaz\PotatoORM\associative.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
71
        }
72
73
        throw NoRecordFoundException::create('There is no record to display');
74
    }
75
76
    /**
77
     * This method create or update record in a database table.
78
     *
79
     * @params void
80
     *
81
     * @throws EmptyArrayException
82
     * @throws NoRecordInsertionException
83
     * @throws NoRecordUpdateException
84
     *
85
     * @return bool true or false;
86
     */
87
    public function save($dbConn = Null)
88
    {
89
        if (is_null($dbConn)) {
90
            $dbConn = new DatabaseConnection();
91
        }
92
93
        $boolCommit = false;
0 ignored issues
show
$boolCommit is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
94
95
        if ($this->properties['id']) {
96
97
            $allData = DatabaseHandler::read($this->properties['id'], self::getClassName(), $dbConn);
98
99
            if ($this->checkIfRecordIsEmpty($allData)) {
100
                $boolCommit = $this->databaseModel->update(['id' => $this->properties['id']], $this->tableName, $this->properties, $dbConn);
101
102
                if ($boolCommit) {
103
                    return true;
104
                }
105
106
                throw NoRecordUpdateException::create('Record not updated successfully');
107
            }
108
109
            throw EmptyArrayException::create("Value passed didn't match any record");
110
        }
111
112
        $boolCommit = $this->databaseModel->create($this->properties, $this->tableName, $dbConn);
113
114
        if ($boolCommit) {
115
            return true;
116
        }
117
118
        throw NoRecordInsertionException::create('Record not created successfully');
119
    }
120
121
    /**
122
     * This method find a record by id.
123
     *
124
     * @params int id
125
     *
126
     * @throws NoArgumentPassedToFunctionException
127
     *
128
     * @return object
129
     */
130
    public static function find($id)
131
    {
132
        $num_args = (int) func_num_args(); // get number of arguments passed to this function
133
        if ($num_args == 0 || $num_args > 1) {
134
            throw NoArgumentPassedToFunctionException::create('Argument missing: only one argument is allowed');
135
        }
136
137
        if ($id == '') {
138
            throw NullArgumentPassedToFunctionException::create('This function expect a value');
139
        }
140
141
        $staticFindInstance = new static();
142
        $staticFindInstance->id = $id == '' ? false : $id;
0 ignored issues
show
The property id does not exist on object<Laztopaz\PotatoORM\BaseModel>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
143
144
        return $staticFindInstance;
145
    }
146
147
    /**
148
     * This method delete a row from the table by the row id.
149
     *
150
     * @params int id
151
     *
152
     * @throws NoRecordDeletionException;
153
     *
154
     * @return bool true or false
155
     */
156
    public static function destroy($id)
157
    {
158
        $boolDeleted = false;
0 ignored issues
show
$boolDeleted is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
159
160
        $num_args = (int) func_num_args(); // get number of arguments passed to this function
161
162
        if ($num_args == 0 || $num_args > 1) {
163
            throw NoArgumentPassedToFunctionException::create('Argument missing: only one argument is allowed');
164
        }
165
166
        $boolDeleted = DatabaseHandler::delete($id, self::getClassName());
167
168
        if ($boolDeleted) {
169
            return true;
170
        }
171
172
        throw NoRecordDeletionException::create('Record deletion unsuccessful because id does not match any record');
173
    }
174
175
   /**
176
    * This method return the current class name
177
    * $params void.
178
    *
179
    * @return classname
180
    */
181
   public static function getClassName()
182
   {
183
       $tableName = preg_split('/(?=[A-Z])/', get_called_class());
184
185
       $className = end($tableName);
186
187
       return self::pluralize(strtolower($className));
188
   }
189
190
    /**
191
     * This method check if the argument passed to this function is an array.
192
     *
193
     * @param $arrayOfRecord
194
     *
195
     * @return bool
196
     */
197
    public function checkIfRecordIsEmpty($arrayOfRecord)
198
    {
199
        if (count($arrayOfRecord) > 0) {
200
            return true;
201
        }
202
203
        return false;
204
    }
205
}
206