Skip to content

Commit

Permalink
Requires PHP 7.2+
Browse files Browse the repository at this point in the history
  • Loading branch information
olvlvl committed Mar 20, 2018
1 parent dc856b5 commit 8cc29f9
Show file tree
Hide file tree
Showing 62 changed files with 728 additions and 974 deletions.
14 changes: 7 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ cache:
directories:
- $COMPOSER_CACHE_DIR
- $HOME/.composer/cache
- $TRAVIS_BUILD_DIR/build
- build

language: php

php:
- 5.6
- 7.0
- 7.1
- 7.2
- nightly

before_script:
- if [[ $TRAVIS_PHP_VERSION != "5.6" ]]; then phpenv config-rm xdebug.ini; fi
matrix:
allow_failures:
- php: nightly

script:
- if [[ $TRAVIS_PHP_VERSION == "5.6" ]]; then make test-coveralls; else make test; fi
- if [[ $TRAVIS_PHP_VERSION == "7.2" ]]; then make test-coveralls; else make test; fi
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
The ICanBoogie/ActiveRecord package is free software.
The icanboogie/activerecord package is free software.
It is released under the terms of the following BSD License.

Copyright (c) 2007-2018 by Olivier Laviale
Expand Down
10 changes: 5 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# customization

PACKAGE_NAME = icanboogie/activerecord
PACKAGE_VERSION = 4.0
PHPUNIT_VERSION = phpunit-5.phar
PACKAGE_VERSION = 5.0
PHPUNIT_VERSION = phpunit-7.phar
PHPUNIT_FILENAME = build/$(PHPUNIT_VERSION)
PHPUNIT = php $(PHPUNIT_FILENAME)

Expand All @@ -20,15 +20,15 @@ update:
autoload: vendor
@composer dump-autoload

test-dependencies: vendor $(PHPUNIT_FILENAME)

$(PHPUNIT_FILENAME):
mkdir -p build
wget https://phar.phpunit.de/$(PHPUNIT_VERSION) -O $(PHPUNIT_FILENAME)

test: test-dependencies
@$(PHPUNIT)

test-dependencies: vendor $(PHPUNIT_FILENAME)

test-coverage: test-dependencies
@mkdir -p build/coverage
@$(PHPUNIT) --coverage-html build/coverage
Expand All @@ -37,7 +37,7 @@ test-coveralls: test-dependencies
@mkdir -p build/logs
COMPOSER_ROOT_VERSION=$(PACKAGE_VERSION) composer require satooshi/php-coveralls
@$(PHPUNIT) --coverage-clover build/logs/clover.xml
php vendor/bin/coveralls -v
php vendor/bin/php-coveralls -v

doc: vendor
@mkdir -p build/docs
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2447,7 +2447,7 @@ already instantiated model.

## Requirements

The package requires PHP 5.6 or later and the [PDO extension](http://php.net/manual/en/intro.pdo.php).
The package requires PHP 7.2 or later and the [PDO extension](http://php.net/manual/en/intro.pdo.php).



Expand Down
9 changes: 7 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
"prefer-stable": true,
"prefer-dist": true,
"require": {
"php": "^5.6 || ^7.0",
"php": "^7.2",
"ext-pdo": "*",
"icanboogie/prototype": "^3.0",
"icanboogie/prototype": "^5.0",
"icanboogie/inflector": "^1.4",
"icanboogie/datetime": "^1.2",
"icanboogie/validate": "^0.2"
Expand All @@ -43,5 +43,10 @@
"files": [
"helpers.php"
]
},
"autoload-dev": {
"psr-4": {
"ICanBoogie\\": "tests"
}
}
}
8 changes: 4 additions & 4 deletions helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,21 @@
*
* @throws ModelNotDefined if the model cannot be found.
*/
function get_model($id)
function get_model(string $id): Model
{
return ModelProvider::provide($id);
}

/**
* Extract the charset and collate from a charset/collate union.
*
* @param $charset_and_collate
* @param string $charset_and_collate
*
* @return array
*/
function extract_charset_and_collate($charset_and_collate)
function extract_charset_and_collate(string $charset_and_collate): array
{
list($charset, $collate) = explode('/', $charset_and_collate) + [ 1 => 'general_ci'];
[ $charset, $collate ] = \explode('/', $charset_and_collate) + [ 1 => 'general_ci'];

return [ $charset, "{$charset}_{$collate}" ];
}
66 changes: 26 additions & 40 deletions lib/ActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,27 @@ class ActiveRecord extends Prototyped
*/
private $model;

protected function get_model(): Model
{
return $this->model
?: $this->model = ActiveRecord\get_model($this->model_id);
}

/**
* Identifier of the model managing the active record.
*
* Note: Due to a PHP bug (or feature), the visibility of the property MUST NOT be private.
* https://bugs.php.net/bug.php?id=40412
*
* @var string
* @var string|null
*/
protected $model_id;

protected function get_model_id(): ?string
{
return $this->model_id;
}

/**
* Initializes the {@link $model} and {@link $model_id} properties.
*
Expand All @@ -72,7 +83,7 @@ public function __construct($model = null)
$model = static::MODEL_ID;
}

if (is_string($model))
if (\is_string($model))
{
$this->model_id = $model;
}
Expand All @@ -83,7 +94,9 @@ public function __construct($model = null)
}
else
{
throw new \InvalidArgumentException("\$model must be an instance of ICanBoogie\\ActiveRecord\\Model or a model identifier. Given:" . (is_object($model) ? get_class($model) : gettype($model)));
throw new \InvalidArgumentException(
"\$model must be an instance of ICanBoogie\\ActiveRecord\\Model or a model identifier. Given:" . (\is_object($model) ? \get_class($model) : \gettype($model))
);
}
}

Expand All @@ -99,7 +112,7 @@ public function __sleep()

unset($properties['model']);

foreach (array_keys($properties) as $property)
foreach (\array_keys($properties) as $property)
{
if ($this->$property instanceof self)
{
Expand All @@ -125,37 +138,16 @@ public function __debugInfo()
return $array;
}

/**
* Returns the model managing the active record.
*
* @return Model
*/
protected function get_model()
{
return $this->model
?: $this->model = ActiveRecord\get_model($this->model_id);
}

/**
* Returns the identifier of the model managing the active record.
*
* @return string
*/
protected function get_model_id()
{
return $this->model_id;
}

/**
* Whether the record is new or not.
*
* @return bool
*/
protected function get_is_new()
protected function get_is_new(): bool
{
$primary = $this->get_model()->primary;

if (is_array($primary))
if (\is_array($primary))
{
foreach ($primary as $property)
{
Expand Down Expand Up @@ -198,7 +190,7 @@ public function save(array $options = [])

$primary = $model->primary;

if (is_array($primary))
if (\is_array($primary))
{
return $model->insert($properties, [ 'on duplicate' => true ]);
}
Expand Down Expand Up @@ -239,27 +231,21 @@ public function save(array $options = [])
* Assert that a record is valid.
*
* @throws RecordNotValid if the record is not valid.
*
* @return $this
*/
public function assert_is_valid()
public function assert_is_valid(): void
{
$errors = $this->validate();

if ($errors)
{
throw new RecordNotValid($this, $errors);
}

return $this;
}

/**
* Creates validation rules.
*
* @return array
*/
public function create_validation_rules()
public function create_validation_rules(): array
{
return [];
}
Expand All @@ -273,7 +259,7 @@ public function create_validation_rules()
*
* @return array The altered persistent properties
*/
protected function alter_persistent_properties(array $properties, Schema $schema)
protected function alter_persistent_properties(array $properties, Schema $schema): array
{
foreach ($properties as $identifier => $value)
{
Expand All @@ -293,7 +279,7 @@ protected function alter_persistent_properties(array $properties, Schema $schema
*
* @param array|string|int $primary_key
*/
protected function update_primary_key($primary_key)
protected function update_primary_key($primary_key): void
{
$model = $this->model;
$property = $model->primary;
Expand All @@ -311,9 +297,9 @@ protected function update_primary_key($primary_key)
*
* @return bool `true` if the record was deleted, `false` otherwise.
*
* @throws \Exception in attempt to delete a record from a model which primary key is empty.
* @throws \LogicException in attempt to delete a record from a model which primary key is empty.
*/
public function delete()
public function delete(): bool
{
$model = $this->model;
$primary = $model->primary;
Expand Down
12 changes: 6 additions & 6 deletions lib/ActiveRecord/ActiveRecordCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,26 @@ interface ActiveRecordCache
*
* @param ActiveRecord $record
*/
public function store(ActiveRecord $record);
public function store(ActiveRecord $record): void;

/**
* Retrieves an {@link ActiveRecord} instance from the cache.
*
* @param int $key
* @param mixed $key
*
* @return ActiveRecord|null
*/
public function retrieve($key);
public function retrieve($key): ?ActiveRecord;

/**
* Eliminates an {@link ActiveRecord} instance from the cache.
*
* @param int $key
* @param mixed $key
*/
public function eliminate($key);
public function eliminate($key): void;

/**
* Clears the cache.
*/
public function clear();
public function clear(): void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ abstract class AbstractActiveRecordCache implements ActiveRecordCache
*/
protected $model;

/**
* @param Model $model
*/
public function __construct(Model $model)
{
$this->model = $model;
Expand Down
10 changes: 5 additions & 5 deletions lib/ActiveRecord/ActiveRecordCache/RuntimeActiveRecordCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ class RuntimeActiveRecordCache extends AbstractActiveRecordCache implements \Ite
*
* @var ActiveRecord[]
*/
protected $records = [];
private $records = [];

/**
* @inheritdoc
*/
public function store(ActiveRecord $record)
public function store(ActiveRecord $record): void
{
$key = $record->{ $this->model->primary };

Expand All @@ -43,7 +43,7 @@ public function store(ActiveRecord $record)
/**
* @inheritdoc
*/
public function retrieve($key)
public function retrieve($key): ?ActiveRecord
{
if (empty($this->records[$key]))
{
Expand All @@ -56,15 +56,15 @@ public function retrieve($key)
/**
* @inheritdoc
*/
public function eliminate($key)
public function eliminate($key): void
{
unset($this->records[$key]);
}

/**
* @inheritdoc
*/
public function clear()
public function clear(): void
{
$this->records = [];
}
Expand Down

0 comments on commit 8cc29f9

Please sign in to comment.