Skip to content

Commit e847c2e

Browse files
authored
Integration fixes (#101)
1 parent 43b8d09 commit e847c2e

File tree

9 files changed

+39
-82
lines changed

9 files changed

+39
-82
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Note that the original website, <http://www.phpactiverecord.org/>, has also fall
1515

1616
http://php-activerecord.github.io/activerecord/
1717

18-
and can be found in in `/docs` if you want to make any changes.
18+
and can be found in `/docs` if you want to make any changes.
1919

2020
## Installation
2121

lib/Column.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,16 @@ public static function castIntegerSafely($value): string|int
117117
return $value;
118118
}
119119

120+
// string representation of a float
121+
elseif ((string) (float) $value === $value && (string) (int) $value !== $value) {
122+
return (int) $value;
123+
}
124+
120125
// If adding 0 to a string causes a float conversion,
121126
// we have a number over PHP_INT_MAX
122-
elseif (is_string($value) && 1 === bccomp($value, (string) PHP_INT_MAX)) {
123-
return $value;
127+
// @phpstan-ignore-next-line
128+
elseif (is_string($value) && is_float($value + 0)) {
129+
return (string) $value;
124130
}
125131

126132
// If a float was passed and is greater than PHP_INT_MAX

lib/Config.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class Config extends Singleton
3838
* });
3939
* ```
4040
*
41-
* This is a singleton class so you can retrieve the {@link Singleton} instance by doing:
41+
* This is a singleton class, so you can retrieve the {@link Singleton} instance by doing:
4242
*
4343
* ```
4444
* $config = ActiveRecord\Config::instance();
@@ -59,7 +59,7 @@ class Config extends Singleton
5959
private bool $logging = false;
6060

6161
/**
62-
* Contains a Logger object that must impelement a log() method.
62+
* Contains a Logger object that must implement a log() method.
6363
*/
6464
private LoggerInterface $logger;
6565

@@ -202,9 +202,9 @@ public function get_logging(): bool
202202
/**
203203
* Returns the logger
204204
*/
205-
public function get_logger(): LoggerInterface
205+
public function get_logger(): LoggerInterface|null
206206
{
207-
return $this->logger;
207+
return $this->logger ?? null;
208208
}
209209

210210
/**

lib/Connection.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ abstract class Connection
5050
/**
5151
* Contains a Logger object that must implement a log() method.
5252
*/
53-
private LoggerInterface $logger;
53+
private LoggerInterface|null $logger;
5454
/**
5555
* The name of the protocol that is used.
5656
*/
@@ -348,9 +348,9 @@ public function eqToken(mixed $value): string
348348
public function query(string $sql, array &$values = [])
349349
{
350350
if ($this->logging) {
351-
$this->logger->info($sql);
351+
$this->logger?->info($sql);
352352
if ($values) {
353-
$this->logger->info(var_export($values, true));
353+
$this->logger?->info(var_export($values, true));
354354
}
355355
}
356356

lib/Model.php

Lines changed: 4 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
use ActiveRecord\Exception\ActiveRecordException;
1111
use ActiveRecord\Exception\ReadOnlyException;
12-
use ActiveRecord\Exception\RecordNotFound;
1312
use ActiveRecord\Exception\RelationshipException;
1413
use ActiveRecord\Exception\UndefinedPropertyException;
1514
use ActiveRecord\Relationship\AbstractRelationship;
@@ -1444,12 +1443,9 @@ public static function __callStatic(string $method, mixed $args): static|null
14441443
/**
14451444
* Enables the use of build|create for associations.
14461445
*
1447-
* @param string $method Name of method
1448-
* @param mixed $args Method args
1449-
*
14501446
* @return mixed An instance of a given {@link AbstractRelationship}
14511447
*/
1452-
public function __call($method, $args)
1448+
public function __call(string $method, mixed $args)
14531449
{
14541450
// check for build|create_association methods
14551451
if (preg_match('/(build|create)_/', $method)) {
@@ -1736,56 +1732,11 @@ public static function last(int $limit = null): static|array|null
17361732
}
17371733

17381734
/**
1739-
* Find records in the database.
1740-
*
1741-
* Finding by the primary key:
1742-
*
1743-
* ```
1744-
* # queries for the model with id=123
1745-
* YourModel::find(123);
1746-
*
1747-
* # queries for model with id in(1,2,3)
1748-
* YourModel::find(1,2,3);
1749-
*
1750-
* # finding by pk accepts an options array
1751-
* YourModel::find(123, ['order' => 'name desc']);
1752-
* ```
1753-
*
1754-
* Finding by using a conditions array:
1755-
*
1756-
* ```
1757-
* YourModel::find('first', ['conditions' => ['name=?','Tito']],
1758-
* 'order' => 'name asc'])
1759-
* YourModel::find('all', ['conditions' => 'amount > 3.14159265']);
1760-
* YourModel::find('all', ['conditions' => ['id in(?)', [1,2,3]]]);
1761-
* ```
1762-
*
1763-
* Finding by using a hash:
1764-
*
1765-
* ```
1766-
* YourModel::find(['name' => 'Tito', 'id' => 1]);
1767-
* YourModel::find('first',['name' => 'Tito', 'id' => 1]);
1768-
* YourModel::find('all',['name' => 'Tito', 'id' => 1]);
1769-
* ```
1770-
*
1771-
* @throws RecordNotFound if no options are passed or finding by pk and no records matched
1772-
*
1773-
* @return Model|Model[]|null
1774-
*
1775-
* The rules for what gets returned are complex, but predictable:
1735+
* @see Relation::find()
17761736
*
1777-
* /-------------------------------------------------------------------------------------------
1778-
* First Argument Return Type Example
1779-
* --------------------------------------------------------------------------------------------
1780-
* int|string static User::find(3);
1781-
* array<string, int|string> static User::find(["name"=>"Philip"]);
1782-
* "first" static|null User::find("first", ["name"=>"Waldo"]);
1783-
* "last" static|null User::find("last", ["name"=>"William"]);
1784-
* "all" static[] User::find("all", ["name"=>"Stephen"]
1785-
* ...int|string static[] User::find(1, 3, 5, 8);
1786-
* list<int|string> static[] User::find([1,3,5,8]);
1737+
* @return static|array<static>
17871738
*/
1788-
public static function find(/* $pk */): Model|array|null
1739+
public static function find(/* $pk */): Model|array
17891740
{
17901741
return static::Relation()->find(...func_get_args());
17911742
}

lib/PhpStan/Relation/RelationReflectionHelper.php

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use PhpParser\Node\Arg;
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Type\ArrayType;
8-
use PHPStan\Type\Constant\ConstantArrayType;
98
use PHPStan\Type\Constant\ConstantStringType;
109
use PHPStan\Type\IntegerType;
1110
use PHPStan\Type\NullType;
@@ -14,18 +13,6 @@
1413

1514
trait RelationReflectionHelper
1615
{
17-
protected function isNumericArray(ConstantArrayType $args): bool
18-
{
19-
$keys = $args->getKeyTypes();
20-
foreach ($keys as $key) {
21-
if (!($key instanceof IntegerType)) {
22-
return false;
23-
}
24-
}
25-
26-
return true;
27-
}
28-
2916
/**
3017
* @return string[]
3118
*/
@@ -55,8 +42,7 @@ protected function computeTypeFromArgs(string $name, array $args, Type $modelTyp
5542
$nullable = false;
5643

5744
if (1 == $numArgs) {
58-
if (!($args[0] instanceof ConstantArrayType)
59-
|| (!$this->isNumericArray($args[0]))) {
45+
if (!$args[0]->isArray()->yes()) {
6046
$single = true;
6147
}
6248
} elseif ($numArgs > 1) {

lib/Relation.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,8 @@ public function __call(string $method, mixed $args): Model|null
177177
return $this->className::create(SQLBuilder::create_hash_from_underscored_string(
178178
$attributes,
179179
$args,
180-
$this->alias_attribute));
180+
$this->alias_attribute
181+
));
181182
}
182183

183184
return null;
@@ -306,7 +307,7 @@ public function reselect(): Relation
306307
*
307308
* @param string|array<string> $joins
308309
*
309-
* @returns Relation<TModel>
310+
* @return Relation<TModel>
310311
*/
311312
public function joins(string|array $joins): Relation
312313
{

test/helpers/config.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@
2323

2424
require_once __DIR__ . '/../../ActiveRecord.php';
2525

26-
// whether or not to run the slow non-crucial tests
26+
// whether to run the slow non-crucial tests
2727
$GLOBALS['slow_tests'] = false;
2828

29-
// whether or not to show warnings when Log or Memcache is missing
29+
// whether to show warnings when Log or Memcache is missing
3030
$GLOBALS['show_warnings'] = true;
3131

3232
if ('false' !== getenv('LOG')) {

test/phpstan/RelationFIndDynamicReturn.php renamed to test/phpstan/RelationFindDynamicReturn.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,20 @@ class RelationFindDynamicReturn
2424

2525
public function __construct()
2626
{
27+
/**
28+
* @var non-empty-array<int>
29+
*/
30+
$ids = [];
31+
2732
$this->book = Book::All()->find(1);
2833
$this->books = Book::All()->find([1, 2]);
34+
$this->books = Book::All()->find($ids);
35+
36+
$books = Book::find($ids);
37+
$this->books = $books;
38+
39+
$mixed = 1;
40+
$books = Book::find($mixed);
41+
$this->book = $books;
2942
}
3043
}

0 commit comments

Comments
 (0)