User
last analyzed

Complexity

Total Complexity 0

Size/Duplication

Total Lines 4
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1
Metric Value
wmc 0
lcom 0
cbo 1
dl 0
loc 4
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 18 and the first side effect is on line 2.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
require 'vendor/autoload.php';
3
4
use Ganga\Potato\Potato;
5
use Ganga\Potato\Connection;
6
7
/**
8
 * NOTE:: Create a test.db database fil
9
 * and run the users.sql dump file in the root of the project
10
 * to create the users table and some records
11
 */
12
13
/**
14
 * It is advisable that classes are created on their own file
15
 * This class definition will connect to the users table
16
 */
17
18
class User extends Potato
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
19
{
20
21
}
22
23
// SQLite config example
24
// Make sure a users table exists
25
// A sample users.sql dump is provided in the root of the project
26
$sqliteConfig = [
27
    "type" => "sqlite",
28
    "database" => "test.db"
29
];
30
31
// Load environment variables
32
$dotenv = new Dotenv\Dotenv(__DIR__);
33
$dotenv->load();
34
35
// MySQL config example
36
// Make sure you have a mysql database connection available
37
// And that the connected database has a users table with id, name, address, company and age
38
// As culumns
39
// Fill in the fields below with the relevant ones
40
$mysqlConfig = [
41
    "type" => getenv('DB_TYPE'),
42
    "database" => getenv('DB_NAME'),
43
    "user" => getenv('DB_USER'),
44
    "password" => getenv('DB_PASS'),
45
    "host" => getenv('DB_HOST')
46
];
47
48
// Make sure you choose the correct config type
49
$conn = new Connection($mysqlConfig);
50
51
// Get all users from the users table
52
$users = User::getAll();
53
echo '<h3>All Users</h3>';
54
prettyPrint($users);
55
56
// Get one user from the users table
57
$user = User::getOne(3);//
58
echo '<h3>One User</h3>';
59
prettyPrint($user);
60
61
// Create a new user
62
$user = new User;
63
$user->name = "Jin Kazama";
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<User>. 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...
64
$user->age = 21;
0 ignored issues
show
Documentation introduced by
The property age does not exist on object<User>. 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...
65
$user->address = "525 Kindaruma";
0 ignored issues
show
Documentation introduced by
The property address does not exist on object<User>. 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...
66
$user->company = "466 Video Productions";
0 ignored issues
show
Documentation introduced by
The property company does not exist on object<User>. 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...
67
$user->save();
68
$users = User::getAll();
69
echo '<h3>All Users</h3>';
70
prettyPrint($users);
71
72
// Edit an existing user
73
$user = User::find(1000);
74
$user->name = "Anthony Steven";
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<User>. 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...
75
$user->save();
76
$users = User::getAll();
77
echo '<h3>All Users (3rd edited)</h3>';
78
prettyPrint($users);
79
80
// Delete a user
81
User::destroy(2);
0 ignored issues
show
Documentation introduced by
2 is of type integer, but the function expects a object<Ganga\Potato\id>.

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...
82
$user_del = User::getAll();
83
echo '<h3>All Users (id 2 deleted)</h3>';
84
prettyPrint($user_del);
85
86
87
function prettyPrint($data)
88
{
89
    echo '<pre>'.print_r($data, true);
90
}
91