Person::getLastname()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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 32 and the first side effect is on line 4.

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
declare(strict_types = 1);
3
4
require_once __DIR__.'/../vendor/autoload.php';
5
6
// this initialisation might look complex, but remember that it's done only once - in your DI container
7
$pdo = new \PDO('sqlite::memory:');
8
$databaseAdapter = new \Agares\MicroORM\PDODbAdapter($pdo);
9
$entityMapper = new \Agares\MicroORM\EntityMapper();
10
$queryAdapter = new \Agares\MicroORM\QueryAdapter($databaseAdapter, $entityMapper);
11
$entityDefinitionCreator = new \Agares\MicroORM\EntityDefinitionCreator(new \Agares\MicroORM\FieldNameMappers\ToUnderscores());
12
13
// let's create some data!
14
$queryAdapter->executeCommand('CREATE TABLE people (firstname TEXT, lastname TEXT, age INT)');
15
$people = [
16
    ['Jeff', 'Lebowski', 30],
17
    ['Bunny', 'Lebowski', 25],
18
    ['The', 'Dude', 50]
19
];
20
21
foreach ($people as $person) {
22
    $parameters = array(
23
        ':firstname' => $person[0],
24
        ':lastname' => $person[1],
25
        ':age' => $person[2]
26
    );
27
    $queryAdapter->executeCommand('INSERT INTO people VALUES(:firstname, :lastname, :age)', $parameters);
28
}
29
30
// time to start the real fun!
31
// Let's create an entity
32
class Person
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...
33
{
34
    /**
35
     * @var string
36
     */
37
    private $firstname;
38
39
    /**
40
     * @var string
41
     */
42
    private $lastname;
43
44
    /**
45
     * @var int
46
     */
47
    private $age;
48
49
    /**
50
     * This constructor will not be called by MicroORM.
51
     *
52
     * @param string $firstname
53
     * @param string $lastname
54
     * @param int $age
55
     */
56
    public function __construct(string $firstname, string $lastname, int $age)
57
    {
58
        $this->firstname = $firstname;
59
        $this->lastname = $lastname;
60
        $this->age = $age;
61
    }
62
63
    public function getFirstname() : string
64
    {
65
        return $this->firstname;
66
    }
67
68
    public function getLastname() : string
69
    {
70
        return $this->lastname;
71
    }
72
73
    public function getAge() : int
74
    {
75
        return $this->age;
76
    }
77
}
78
79
// And now we can query the DB
80
// The types of properties will be inferred from getters
81
$people = $queryAdapter->executeQuery('SELECT firstname, lastname, age FROM people', $entityDefinitionCreator->create(Person::class));
82
83
foreach ($people as $person) {
84
    /** @var Person $person */
85
    printf('%s %s (age %d)%s', $person->getFirstname(), $person->getLastname(), $person->getAge(), PHP_EOL);
86
}