Issues (25)

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/Eloquent/Model.php (10 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
namespace lroman242\LaravelCassandra\Eloquent;
4
5
use Carbon\Carbon;
6
use Cassandra\Rows;
7
use Cassandra\Timestamp;
8
use lroman242\LaravelCassandra\CassandraTypesTrait;
9
use lroman242\LaravelCassandra\Collection;
10
use lroman242\LaravelCassandra\Query\Builder as QueryBuilder;
11
use Illuminate\Database\Eloquent\Model as BaseModel;
12
13
abstract class Model extends BaseModel
14
{
15
    use CassandraTypesTrait;
16
17
    /**
18
     * The connection name for the model.
19
     *
20
     * @var string
21
     */
22
    protected $connection = 'cassandra';
23
24
    /**
25
     * Indicates if the IDs are auto-incrementing.
26
     * This is not possible in cassandra so we override this
27
     *
28
     * @var bool
29
     */
30
    public $incrementing = false;
31
32
    /**
33
     * @inheritdoc
34
     */
35 55
    public function newEloquentBuilder($query)
36
    {
37 55
        return new Builder($query);
38
    }
39
40
    /**
41
     * @inheritdoc
42
     */
43 55
    protected function newBaseQueryBuilder()
44
    {
45 55
        $connection = $this->getConnection();
46
47 55
        return new QueryBuilder($connection, null, $connection->getPostProcessor());
0 ignored issues
show
$connection of type object<Illuminate\Database\Connection> is not a sub-type of object<lroman242\LaravelCassandra\Connection>. It seems like you assume a child class of the class Illuminate\Database\Connection 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...
$connection->getPostProcessor() is of type object<Illuminate\Databa...y\Processors\Processor>, but the function expects a null|object<lroman242\La...sandra\Query\Processor>.

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...
48
    }
49
50
    /**
51
     * @inheritdoc
52
     */
53 13
    public function freshTimestamp()
54
    {
55 13
        return new Timestamp();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Cassandra\Timestamp(); (Cassandra\Timestamp) is incompatible with the return type of the parent method Illuminate\Database\Eloquent\Model::freshTimestamp of type Illuminate\Support\Carbon.

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...
56
    }
57
58
    /**
59
     * @inheritdoc
60
     */
61 13
    public function fromDateTime($value)
62
    {
63
        // If the value is already a Timestamp instance, we don't need to parse it.
64 13
        if ($value instanceof Timestamp) {
0 ignored issues
show
The class Cassandra\Timestamp does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
65 13
            return $value;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $value; (Cassandra\Timestamp) is incompatible with the return type of the parent method Illuminate\Database\Eloquent\Model::fromDateTime of type integer|string.

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...
66
        }
67
68
        // Let Eloquent convert the value to a DateTime instance.
69 1
        if (!$value instanceof \DateTime) {
70 1
            $value = parent::asDateTime($value);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (asDateTime() instead of fromDateTime()). Are you sure this is correct? If so, you might want to change this to $this->asDateTime().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
71
        }
72
73 1
        return new Timestamp($value->getTimestamp() * 1000);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Cassandra\Ti...getTimestamp() * 1000); (Cassandra\Timestamp) is incompatible with the return type of the parent method Illuminate\Database\Eloquent\Model::fromDateTime of type integer|string.

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...
74
    }
75
76
    /**
77
     * @inheritdoc
78
     */
79 6
    protected function asDateTime($value)
80
    {
81
        // Convert UTCDateTime instances.
82 6
        if ($value instanceof Timestamp) {
0 ignored issues
show
The class Cassandra\Timestamp does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
83 6
            return Carbon::instance($value->toDateTime());
84
        }
85
86
        return parent::asDateTime($value);
87
    }
88
89
    /**
90
     * Get the table qualified key name.
91
     * Cassandra does not support the table.column annotation so
92
     * we override this
93
     *
94
     * @return string
95
     */
96 8
    public function getQualifiedKeyName()
97
    {
98 8
        return $this->getKeyName();
99
    }
100
101
    /**
102
     * Qualify the given column name by the model's table.
103
     *
104
     * @param  string  $column
105
     * @return string
106
     */
107 2
    public function qualifyColumn($column)
108
    {
109 2
        return $column;
110
    }
111
112
    /**
113
     * @inheritdoc
114
     */
115 55
    public function __call($method, $parameters)
116
    {
117
        // Unset method
118 55
        if ($method == 'unset') {
119
            return call_user_func_array([$this, 'drop'], $parameters);
120
        }
121
122 55
        return parent::__call($method, $parameters);
123
    }
124
125
    /**
126
     * Create a new Eloquent Collection instance.
127
     *
128
     * @param  Rows|array  $rows
129
     *
130
     * @return Collection
131
     *
132
     * @throws \Exception
133
     */
134 50
    public function newCassandraCollection($rows)
135
    {
136 50
        if (!is_array($rows) && !$rows instanceof Rows) {
0 ignored issues
show
The class Cassandra\Rows does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
137
            throw new \Exception('Wrong type to create collection');//TODO: customize error
138
        }
139
140 50
        $items = [];
141 50
        foreach ($rows as $row) {
142 47
            $items[] = $this->newFromBuilder($row);
143
        }
144
145 50
        $collection = new Collection($items);
146
147 50
        if ($rows instanceof Rows) {
0 ignored issues
show
The class Cassandra\Rows does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
148 19
            $collection->setRowsInstance($rows);
149
        }
150
151 50
        return $collection;
152
    }
153
154
    /**
155
     * Determine if the new and old values for a given key are equivalent.
156
     *
157
     * @param  string $key
158
     * @param  mixed $current
159
     * @return bool
160
     */
161 13
    public function originalIsEquivalent($key, $current)
162
    {
163 13
        if (!array_key_exists($key, $this->original)) {
164 13
            return false;
165
        }
166
167 2
        $original = $this->getOriginal($key);
168
169 2
        if ($current === $original) {
170 2
            return true;
171 2
        } elseif (is_null($current)) {
172
            return false;
173 2
        } elseif ($this->isDateAttribute($key)) {
174 2
            return $this->fromDateTime($current) ===
175 2
                $this->fromDateTime($original);
176 1
        } elseif ($this->hasCast($key)) {
177
            return $this->castAttribute($key, $current) ===
178
                $this->castAttribute($key, $original);
179 1
        } elseif ($this->isCassandraValueObject($current)) {
180
            return $this->valueFromCassandraObject($current) ===
181
                $this->valueFromCassandraObject($original);
182
        }
183
184 1
        return is_numeric($current) && is_numeric($original)
185 1
            && strcmp((string) $current, (string) $original) === 0;
186
    }
187
188
    /**
189
     * Get the value of the model's primary key.
190
     *
191
     * @return mixed
192
     */
193 16
    public function getKey()
194
    {
195 16
        $value = $this->getAttribute($this->getKeyName());
196
197 16
        if ($this->isCassandraValueObject($value)) {
198
            return $this->valueFromCassandraObject($this->getAttribute($this->getKeyName()));
199
        }
200
201 16
        return $value;
202
    }
203
204
}
205