Skip to content

Commit f4f592b

Browse files
authored
Merge pull request #73 from pfwd/72-phpstan
Fixing PHPStan issues
2 parents 45b2923 + f7d66e2 commit f4f592b

File tree

9 files changed

+137
-112
lines changed

9 files changed

+137
-112
lines changed

src/DB/Connection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function open():PDO
3939
{
4040
$creds = $this->getCreds();
4141

42-
if(false === self::$conn instanceof PDO) {
42+
if(!self::$conn instanceof PDO) {
4343
$username = $creds['username'];
4444
$password = $creds['password'];
4545
$host = $creds['host'];

src/DB/QueryBuilder.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,19 +70,19 @@ public static function insertOrUpdate(array $data, string $table, array $where =
7070
}
7171

7272
/**
73-
* @param $table
73+
* @param string $table
7474
* @return string
7575
*/
76-
public static function findOneBy($table)
76+
public static function findOneBy(string $table)
7777
{
7878
return "SELECT * FROM `".$table."` WHERE id=:id";
7979
}
8080

8181
/**
82-
* @param $table
82+
* @param string $table
8383
* @return string
8484
*/
85-
public static function findAll($table)
85+
public static function findAll(string $table)
8686
{
8787
return "SELECT * FROM `".$table."`";
8888
}

src/Entity/AbstractEntity.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
abstract class AbstractEntity
77
{
88
/**
9-
* @var int
9+
* @var int|null
1010
*/
1111
protected $id;
1212

src/Manager/InvoiceManager.php

Lines changed: 8 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,26 @@
11
<?php
22
namespace App\Manager;
33

4-
use App\DB\Connection;
54
use App\DB\QueryBuilder;
65
use App\Entity\Type\Invoice;
76
use App\Entity\Type\Status;
87
use App\Hydration\StatusHydrator;
9-
use App\Repository\Type\Status as Repository;
8+
use App\Repository\Type\InvoiceRepository;
109

1110
class InvoiceManager extends AbstractManager
1211
{
1312
/**
14-
* @var Repository
13+
* @var InvoiceRepository
1514
*/
1615
private $repository;
1716

1817
/**
19-
* @var Connection
18+
* InvoiceManager constructor.
19+
* @param InvoiceRepository $repository
2020
*/
21-
private $connection;
22-
23-
/**
24-
* CustomerManager constructor.
25-
* @param Connection $connection
26-
*/
27-
public function __construct(Connection $connection)
21+
public function __construct(InvoiceRepository $repository)
2822
{
29-
$this->connection = $connection;
23+
$this->repository = $repository;
3024
}
3125

3226

@@ -47,35 +41,7 @@ public function findOne(int $id): Status
4741
*/
4842
public function save(Invoice $entity):Invoice
4943
{
50-
$data = [
51-
'reference' => $entity->getReference(),
52-
'total' => $entity->getTotal(),
53-
'vat' => $entity->getVat()
54-
];
55-
if ($entity->getStatus() instanceof Status){
56-
$data['status_id'] = $entity->getStatus()->getId();
57-
}
58-
$where = [];
59-
if(null !== $entity->getId()) {
60-
$where['id'] = $entity->getId();
61-
}
62-
63-
$table = 'invoice';
64-
65-
$sql = QueryBuilder::insertOrUpdate($data, $table, $where);
66-
67-
if (null !== $entity->getId()) {
68-
$data['id'] = $entity->getId();
69-
}
70-
$dbCon = $this->connection->open();
71-
72-
$statement = $dbCon->prepare($sql);
73-
$statement->execute(array_values($data));
74-
75-
if(null === $entity->getId()){
76-
$entity->setId($dbCon->lastInsertId());
77-
}
78-
79-
return $entity;
44+
$savedEntity = $this->repository->save($entity);
45+
return $savedEntity;
8046
}
8147
}

src/Repository/Type/CustomerRepository.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function save(Customer $entity) {
5454
$statement->execute(array_values($data));
5555

5656
if (null === $entity->getId()) {
57-
$entity->setId($dbCon->lastInsertId());
57+
$entity->setId((int) $dbCon->lastInsertId());
5858
}
5959

6060
return $entity;
@@ -73,8 +73,10 @@ public function findAll():array
7373
$statement->execute();
7474
$rows = $statement->fetchAll();
7575

76-
foreach($rows as $row) {
77-
$results[] = CustomerHydrator::hydrate($row);
76+
if(is_array($rows)) {
77+
foreach($rows as $row) {
78+
$results[] = CustomerHydrator::hydrate($row);
79+
}
7880
}
7981

8082
return $results;

src/Repository/Type/Invoice.php

Lines changed: 0 additions & 37 deletions
This file was deleted.
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
namespace App\Repository\Type;
4+
5+
use App\DB\Connection;
6+
use App\DB\QueryBuilder;
7+
use App\Entity\Type\Invoice;
8+
use App\Entity\Type\Status;
9+
use App\Repository\AbstractRepository;
10+
11+
class InvoiceRepository extends AbstractRepository
12+
{
13+
private $connection;
14+
15+
public function __construct(Connection $connection)
16+
{
17+
$this->connection = $connection;
18+
}
19+
20+
/**
21+
* @param int $id
22+
* @return mixed
23+
*/
24+
public function findOne(int $id)
25+
{
26+
27+
}
28+
29+
/**
30+
* @param array $options
31+
* @return mixed
32+
*/
33+
public function findOneBy(array $options)
34+
{
35+
// TODO: Implement findOneBy() method.
36+
}
37+
38+
/**
39+
* @param array $options
40+
* @return mixed
41+
*/
42+
public function findAllBy(array $options)
43+
{
44+
// TODO: Implement findAllBy() method.
45+
}
46+
47+
/**
48+
* @param Invoice $entity
49+
* @return Invoice
50+
*/
51+
public function save(Invoice $entity):Invoice
52+
{
53+
54+
$data = [
55+
'reference' => $entity->getReference(),
56+
'total' => $entity->getTotal(),
57+
'vat' => $entity->getVat()
58+
];
59+
if ($entity->getStatus() instanceof Status){
60+
$data['status_id'] = $entity->getStatus()->getId();
61+
}
62+
$where = [];
63+
if(null !== $entity->getId()) {
64+
$where['id'] = $entity->getId();
65+
}
66+
67+
$table = 'invoice';
68+
69+
$sql = QueryBuilder::insertOrUpdate($data, $table, $where);
70+
71+
if (null !== $entity->getId()) {
72+
$data['id'] = $entity->getId();
73+
}
74+
$dbCon = $this->connection->open();
75+
76+
$statement = $dbCon->prepare($sql);
77+
$statement->execute(array_values($data));
78+
79+
if(null === $entity->getId()){
80+
$entity->setId($dbCon->lastInsertId());
81+
}
82+
83+
return $entity;
84+
}
85+
86+
}

src/Repository/Type/StatusRepository.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function __construct(Connection $connection)
2525

2626
/**
2727
* @param int $id
28-
* @return mixed
28+
* @return null|Status
2929
*/
3030
public function findOne(int $id):? Status
3131
{
@@ -58,8 +58,10 @@ public function findAll():array
5858
$statement->execute();
5959
$rows = $statement->fetchAll();
6060

61-
foreach($rows as $row) {
62-
$results[] = StatusHydrator::hydrate($row);
61+
if(is_array($rows)) {
62+
foreach($rows as $row) {
63+
$results[] = StatusHydrator::hydrate($row);
64+
}
6365
}
6466

6567
return $results;
@@ -111,7 +113,7 @@ public function save(Status $entity) {
111113
$statement->execute(array_values($data));
112114

113115
if (null === $entity->getId()) {
114-
$entity->setId($dbCon->lastInsertId());
116+
$entity->setId((int) $dbCon->lastInsertId());
115117
}
116118

117119
return $entity;

0 commit comments

Comments
 (0)