Skip to content

Commit 70d412c

Browse files
committed
Apply SA fixes
1 parent 0c4d79d commit 70d412c

File tree

7 files changed

+62
-44
lines changed

7 files changed

+62
-44
lines changed

phpstan.neon.dist

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ parameters:
1010
- src/Config/Routes.php
1111
- src/Views/*
1212
ignoreErrors:
13-
- '#Cannot access property [\$a-z_]+ on (array|object)#'
14-
- '#Unsafe usage of new static\(\)*#'
1513
universalObjectCratesClasses:
1614
- CodeIgniter\Entity
1715
- Faker\Generator

src/Audits.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
<?php namespace Tatter\Audits;
22

3-
use CodeIgniter\Config\BaseConfig;
43
use Tatter\Audits\Models\AuditModel;
4+
use Tatter\Audits\Config\Audits as AuditsConfig;
55

66
/*** CLASS ***/
77
class Audits
88
{
99
/**
1010
* Our configuration instance.
1111
*
12-
* @var \Tatter\Audits\Config\Audits
12+
* @var AuditsConfig
1313
*/
1414
protected $config;
1515

@@ -23,9 +23,9 @@ class Audits
2323
/**
2424
* Store the configuration
2525
*
26-
* @param BaseConfig $config The Audits configuration to use
26+
* @param AuditsConfig $config The Audits configuration to use
2727
*/
28-
public function __construct(BaseConfig $config)
28+
public function __construct(AuditsConfig $config)
2929
{
3030
$this->config = $config;
3131
}
@@ -58,7 +58,7 @@ public function getQueue(): array
5858
/**
5959
* Add an audit row to the queue
6060
*
61-
* @param array|null The row to cache for insert
61+
* @param array|null $audit The row to cache for insert
6262
*/
6363
public function add(array $audit = null)
6464
{

src/Config/Services.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
<?php namespace Tatter\Audits\Config;
22

3+
use Config\Services as BaseServices;
34
use Tatter\Audits\Audits;
5+
use Tatter\Audits\Config\Audits as AuditsConfig;
46

5-
class Services extends \Config\Services
7+
class Services extends BaseServices
68
{
7-
public static function audits(BaseConfig $config = null, bool $getShared = true)
9+
/**
10+
* @param AuditsConfig|null $config
11+
* @param bool $getShared
12+
*
13+
* @return Audits
14+
*/
15+
public static function audits(AuditsConfig $config = null, bool $getShared = true): Audits
816
{
917
if ($getShared)
1018
{

src/Traits/AuditsTrait.php

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,61 @@
11
<?php namespace Tatter\Audits\Traits;
22

3-
use CodeIgniter\Config\Services;
43
use Tatter\Audits\Models\AuditModel;
54

65
/*** CLASS ***/
76
trait AuditsTrait
87
{
9-
// takes an array of model $returnTypes and returns an array of Audits, arranged by object and event
10-
// optionally filter by $events (string or array of strings)
8+
/**
9+
* Takes an array of model $returnTypes
10+
* and returns an array of Audits,
11+
* arranged by object and event.
12+
* Optionally filter by $events
13+
* (string or array of strings).
14+
*
15+
* @param array $objects
16+
* @param array|string|null $events
17+
*
18+
* @internal Due to a typo this function has never worked in a released version.
19+
* It will be refactored soon without announcing a new major release
20+
* so do not build on the signature or functionality.
21+
*/
1122
public function getAudits(array $objects, $events = null): array
1223
{
1324
if (empty($objects))
14-
return null;
15-
16-
// get the primary keys from the objects
25+
{
26+
return [];
27+
}
28+
29+
// Get the primary keys from the objects
1730
$objectIds = array_column($objects, $this->primaryKey);
18-
19-
$audits = new AuditModel();
20-
$query = $query->where('source', $this->table)
21-
->whereIn('source_id', $objectIds);
31+
32+
// Start the query
33+
$query = model(AuditModel::class)->where('source', $this->table)->whereIn('source_id', $objectIds);
34+
2235
if (is_string($events))
36+
{
2337
$query = $query->where('event', $events);
38+
}
2439
elseif (is_array($events))
40+
{
2541
$query = $query->whereIn('event', $events);
42+
}
2643

27-
// index by objectId, event
28-
$array = [ ];
29-
while ($audit = $query->getUnbufferedRow()):
44+
// Index by objectId, event
45+
$array = [];
46+
while ($audit = $query->getUnbufferedRow())
47+
{
3048
if (empty($array[$audit->{$this->primaryKey}]))
31-
$array[$audit->{$this->primaryKey}] = [ ];
32-
49+
{
50+
$array[$audit->{$this->primaryKey}] = [];
51+
}
3352
if (empty($array[$audit->{$this->primaryKey}][$audit->event]))
34-
$array[$audit->{$this->primaryKey}][$audit->event] = [ ];
53+
{
54+
$array[$audit->{$this->primaryKey}][$audit->event] = [];
55+
}
3556

3657
$array[$audit->{$this->primaryKey}][$audit->event][] = $audit;
37-
endwhile;
58+
}
3859

3960
return $array;
4061
}
@@ -47,11 +68,11 @@ protected function auditInsert(array $data)
4768

4869
$audit = [
4970
'source' => $this->table,
50-
'source_id' => $this->db->insertID(),
71+
'source_id' => $this->db->insertID(), // @phpstan-ignore-line
5172
'event' => 'insert',
5273
'summary' => count($data['data']) . ' fields',
5374
];
54-
Services::audits()->add($audit);
75+
service('audits')->add($audit);
5576

5677
return $data;
5778
}
@@ -65,7 +86,7 @@ protected function auditUpdate(array $data)
6586
'event' => 'update',
6687
'summary' => count($data['data']) . ' fields',
6788
];
68-
Services::audits()->add($audit);
89+
service('audits')->add($audit);
6990

7091
return $data;
7192
}
@@ -85,7 +106,7 @@ protected function auditDelete(array $data)
85106
];
86107

87108
// add an entry for each ID
88-
$audits = Services::audits();
109+
$audits = service('audits');
89110
foreach ($data['id'] as $id):
90111
$audit['source_id'] = $id;
91112
$audits->add($audit);

tests/_support/Database/Migrations/2019-09-02-092335_create_test_tables.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ class CreateTestTables extends Migration
66
{
77
public function up()
88
{
9-
$this->db->disableForeignKeyChecks();
10-
119
// Widgets
1210
$fields = [
1311
'name' => ['type' => 'varchar', 'constraint' => 31],
@@ -27,16 +25,10 @@ public function up()
2725
$this->forge->addKey('created_at');
2826

2927
$this->forge->createTable('widgets');
30-
31-
$this->db->enableForeignKeyChecks();
3228
}
3329

3430
public function down()
3531
{
36-
$this->db->disableForeignKeyChecks();
37-
3832
$this->forge->dropTable('widgets');
39-
40-
$this->db->enableForeignKeyChecks();
4133
}
4234
}

tests/_support/DatabaseTestCase.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use CodeIgniter\Test\Fabricator;
55
use Config\Services;
66
use Tatter\Audits\Audits;
7+
use Tatter\Audits\Config\Audits as AuditsConfig;
78
use Tests\Support\Models\WidgetModel;
89

910
class DatabaseTestCase extends CIDatabaseTestCase
@@ -18,21 +19,21 @@ class DatabaseTestCase extends CIDatabaseTestCase
1819
/**
1920
* Our configuration
2021
*
21-
* @var Tatter\Audits\Config\Audits
22+
* @var AuditsConfig
2223
*/
2324
protected $config;
2425

2526
/**
2627
* Instance of the test model
2728
*
28-
* @var Tests\Support\Models\WidgetModel
29+
* @var WidgetModel
2930
*/
3031
protected $model;
3132

3233
/**
3334
* Instance of the fabricator primed with our model
3435
*
35-
* @var CodeIgniter\Test\Fabricator
36+
* @var Fabricator
3637
*/
3738
protected $fabricator;
3839

@@ -57,8 +58,6 @@ public function setUp(): void
5758
* Asserts that an audit with the given properties is in the queue
5859
*
5960
* @param array $values Array of values to confirm
60-
*
61-
* @return self
6261
*/
6362
public function seeAudit(array $values)
6463
{

tests/_support/Models/WidgetModel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class WidgetModel extends Model
2626
*
2727
* @param Generator $faker
2828
*
29-
* @return stdClass
29+
* @return object
3030
*/
3131
public function fake(Generator &$faker): object
3232
{

0 commit comments

Comments
 (0)