Skip to content

Commit

Permalink
Implement PHP 7.4 features
Browse files Browse the repository at this point in the history
  • Loading branch information
iquito committed Apr 24, 2020
1 parent 8a7d315 commit 40b17f2
Show file tree
Hide file tree
Showing 43 changed files with 355 additions and 595 deletions.
13 changes: 6 additions & 7 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
/tests export-ignore
/examples export-ignore
/docker export-ignore
/vendor-bin export-ignore
/.editorconfig export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/phpunit.xml.dist export-ignore
/phpcs.xml.dist export-ignore
/captainhook.json export-ignore
/.travis.yml export-ignore
/ruleset.xml export-ignore
/.editorconfig export-ignore
/captainhook.json export-ignore
/phpstan.neon export-ignore
/phpstan_base.neon export-ignore
/phpstan-baseline.neon export-ignore
/phpunit.xml.dist export-ignore
/psalm.xml export-ignore
/psalm-baseline.xml export-ignore
/vendor-bin export-ignore
/ruleset.xml export-ignore
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
/vendor
/vendor-bin/**/vendor
/vendor-bin/**/composer.lock
/.phpunit*
/.phpunit.result.cache
/.phpcs-cache
/tests/_output
/tests/_reports
/build
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ env:
- GIT_COMMITTED_AT=$(if [ "$TRAVIS_PULL_REQUEST" == "false" ]; then git log -1 --pretty=format:%ct; else git log -1 --skip 1 --pretty=format:%ct; fi)
language: php
php:
- '7.2'
- '7.3'
- '7.4'

before_script:
Expand Down
107 changes: 33 additions & 74 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,70 +43,48 @@ use Squirrel\Entities\Annotation\Field;
class User
{
/**
* @Field("user_id", type="int", autoincrement=true)
*
* @var integer
* @Field("user_id", autoincrement=true)
*/
private $userId = 0;
private int $userId;

/**
* @Field("active", type="bool")
*
* @var bool
* @Field("active")
*/
private $active = false;
private bool $active;

/**
* @Field("street_name", nullable=true)
*
* @var string|null
* @Field("street_name")
*/
private $streetName;
private ?string $streetName;

/**
* @Field("street_number", nullable=true)
*
* @var string|null
* @Field("street_number")
*/
private $streetNumber;
private ?string $streetNumber;

/**
* @Field("city")
*
* @var string
*/
private $city = '';
private string $city;

/**
* @Field("balance", type="float")
*
* @var float
* @Field("balance")
*/
private $balance = 0;
private float $balance;

/**
* @Field("picture_file", type="blob", nullable=true)
*
* @var string|null
* @Field("picture_file", blob=true)
*/
private $picture;
private ?string $picture;

/**
* @Field("visits", type="int")
*
* @var integer
* @Field("visits")
*/
private $visitsNumber = 0;
private int $visitsNumber;
}
```

The class is defined as an entity with the table name, and each class property is defined as a table field with the column name in the database, a type, if it is nullable and if it is an autoincrement column (called SERIAL in Postgres). The possible types are:

- `string`, which is the default if none is specified
- `int` for integer
- `bool` for boolean
- `float` for floating point number
- `blob` for a binary large object, called "blob" in most databases or "bytea" in Postgres, for example to store an image or file
The class is defined as an entity with the table name, and each class property is defined as a table field with the column name in the database, where the type is taken from the PHP property type (string, int, float, bool). If the property type is nullable, the column type is assumed to be nullable too. You can also define if it is an autoincrement column (called SERIAL in Postgres) and if it is a blob column (binary large object, called "blob" in most databases or "bytea" in Postgres).

Whether the class properties are private, protected or public does not matter, you can choose whatever names you want, and you can design the rest of the class however you want. You can even make the classes read-only, by having private properties and only defining getters - see [Read-only entity objects](#read-only-entity-objects) for more details on why you would want to do that.

Expand Down Expand Up @@ -492,25 +470,19 @@ use Squirrel\Entities\Annotation\Field;
class Visit
{
/**
* @Field("visit_id", type="int", autoincrement=true)
*
* @var integer
* @Field("visit_id", autoincrement=true)
*/
private $visitId = 0;
private int $visitId = 0;

/**
* @Field("user_id", type="int")
*
* @var integer
* @Field("user_id")
*/
private $userId = 0;
private int $userId = 0;

/**
* @Field("created_timestamp", type="int")
*
* @var integer
* @Field("created_timestamp")
*/
private $timestamp = 0;
private int $timestamp = 0;
}
```

Expand Down Expand Up @@ -784,32 +756,28 @@ use Squirrel\Entities\Annotation\Field;
class User
{
/**
* @Field("user_id", type="int", autoincrement=true)
*
* @var integer
* @Field("user_id", autoincrement=true)
*/
private $userId = 0;
private int $userId = 0;

/**
* @Field("active", type="bool")
*
* @var bool
* @Field("active")
*/
private $active = false;
private bool $active = false;

/**
* @Field("note_data")
*
* @var string JSON data in the database
*/
private $notes = '';
private string $notes = '';

/**
* @Field("created")
*
* @var string datetime in the database
*/
private $createDate = '';
private string $createDate = '';

public function getUserId(): int
{
Expand Down Expand Up @@ -842,15 +810,8 @@ namespace Application\Value;

class GeoPoint
{
/**
* @var float
*/
private $lat = 0;

/**
* @var float
*/
private $lng = 0;
private float $lat = 0;
private float $lng = 0;

public function __construct(float $lat, float $lng)
{
Expand Down Expand Up @@ -883,18 +844,16 @@ use Squirrel\Entities\Annotation\Field;
class UserLocation
{
/**
* @Field("user_id", type="int")
*
* @var integer
* @Field("user_id")
*/
private $userId = 0;
private int $userId = 0;

/**
* @Field("location")
*
* @var string "point" in Postgres
*/
private $locationPoint = '';
private string $locationPoint = '';

public function getUserId(): int
{
Expand Down
19 changes: 14 additions & 5 deletions bin/squirrel_repositories_generate
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,22 @@ AnnotationRegistry::registerLoader(array($loader, 'loadClass'));

// Define the necessary command line options and defaults
$inputDefinition = new InputDefinition();
$inputDefinition->addOption(new InputOption(
'verbose',
'v',
InputOption::VALUE_NONE,
'Verbose mode, showing all generated repositories' // Description
));
$inputDefinition->addOption(new InputOption(
'source-dir',
null,
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED,
'Source directories (relative to current directory) where entities will be searched recursively'
'Source directories (relative to current directory) where entities will be searched recursively' // Description
));

$input = new ArgvInput(null, $inputDefinition);
$srcDirectories = $input->getOption('source-dir');
$isVerbose = $input->getOption('verbose');

// Execute command to generate repositories
$cmd = new \Squirrel\Entities\Generate\RepositoriesGenerateCommand(
Expand All @@ -45,8 +52,10 @@ $cmd = new \Squirrel\Entities\Generate\RepositoriesGenerateCommand(
);
$log = $cmd();

// Add summary of processed entities
$log[] = "\n" . count($log) . ' entities found for which repositories were generated.' . "\n";
// Show detailed log
if ($isVerbose === true) {
echo implode("\n", $log);
}

// Show log
echo implode("\n", $log);
// Show summary
echo "\n" . count($log) . ' entities found for which repositories were generated.' . "\n";
15 changes: 8 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@
}
],
"require": {
"php": "^7.2",
"symfony/console": "^4.0|^5.0",
"symfony/finder": "^4.0|^5.0",
"php": "^7.4",
"symfony/console": "^5.0",
"symfony/finder": "^5.0",
"doctrine/annotations": "^1.4",
"squirrelphp/queries": "^0.9"
},
"require-dev": {
"bamarni/composer-bin-plugin": "^1.3",
"captainhook/plugin-composer": "^4.0",
"captainhook/plugin-composer": "^5.0",
"mockery/mockery": "^1.0"
},
"suggest": {
Expand All @@ -53,15 +53,16 @@
},
"scripts": {
"phpstan": "vendor/bin/phpstan analyse",
"phpstan_base": "vendor/bin/phpstan analyse --configuration phpstan_base.neon --error-format baselineNeon > phpstan-baseline.neon",
"phpstan_base": "vendor/bin/phpstan analyse --generate-baseline",
"phpstan_clear": "vendor/bin/phpstan clear-result-cache",
"psalm": "vendor/bin/psalm --show-info=false --diff",
"psalm_full": "vendor/bin/psalm --show-info=false",
"psalm_base": "vendor/bin/psalm --set-baseline=psalm-baseline.xml",
"phpunit": "vendor/bin/phpunit --colors=always",
"phpunit_clover": "vendor/bin/phpunit --coverage-text --coverage-clover build/logs/clover.xml",
"phpcs": "vendor/bin/phpcs --standard=ruleset.xml --extensions=php --cache --ignore=tests/TestEntities src bin tests",
"phpcsfix": "vendor/bin/phpcbf --standard=ruleset.xml --extensions=php --cache --ignore=tests/TestEntities src bin tests",
"codecoverage": "vendor/bin/phpunit --coverage-html tests/_reports",
"phpcs": "vendor/bin/phpcs --standard=ruleset.xml --extensions=php --cache=.phpcs-cache --colors src",
"phpcsfix": "vendor/bin/phpcbf --standard=ruleset.xml --extensions=php --cache=.phpcs-cache src",
"binupdate": "@composer bin all update --ansi",
"bininstall": "@composer bin all install --ansi"
}
Expand Down
4 changes: 2 additions & 2 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
parameters:
ignoreErrors:
-
message: "#^Method Squirrel\\\\Entities\\\\Action\\\\SelectEntries\\:\\:getOneEntry\\(\\) has no return typehint specified\\.$#"
message: "#^Call to an undefined method ReflectionType\\:\\:getName\\(\\)\\.$#"
count: 1
path: src/Action/SelectEntries.php
path: src/Annotation/EntityProcessor.php

-
message: "#^Parameter \\#1 \\$query of method Squirrel\\\\Queries\\\\DBInterface\\:\\:fetchOne\\(\\) expects array\\<string, array\\<int\\|string, mixed\\>\\|bool\\|int\\|string\\>\\|string, array\\<string, array\\<int\\|string, mixed\\>\\|string\\|true\\> given\\.$#"
Expand Down
6 changes: 0 additions & 6 deletions phpstan_base.neon

This file was deleted.

8 changes: 3 additions & 5 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="3.11.2@d470903722cfcbc1cd04744c5491d3e6d13ec3d9">
<file src="src/Action/SelectEntries.php">
<MissingReturnType occurrences="1">
<code>getOneEntry</code>
</MissingReturnType>
</file>
<file src="src/MultiRepositoryReadOnly.php">
<MissingConstructor occurrences="1">
<code>$db</code>
</MissingConstructor>
<PossiblyInvalidArgument occurrences="1">
<code>$sanitizedOptions['query']</code>
</PossiblyInvalidArgument>
Expand Down
8 changes: 0 additions & 8 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,4 @@
<directory name="vendor" />
</ignoreFiles>
</projectFiles>

<issueHandlers>
<MissingConstructor>
<errorLevel type="suppress">
<file name="src/MultiRepositoryReadOnly.php" />
</errorLevel>
</MissingConstructor>
</issueHandlers>
</psalm>
9 changes: 3 additions & 6 deletions src/Action/CountEntries.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,17 @@
*/
class CountEntries implements ActionInterface
{
/**
* @var RepositoryReadOnlyInterface Repository we call to execute the built query
*/
private $repository;
private RepositoryReadOnlyInterface $repository;

/**
* @var array<int|string,mixed> WHERE restrictions in query
*/
private $where = [];
private array $where = [];

/**
* @var bool Whether the SELECT query should block the scanned entries
*/
private $blocking = false;
private bool $blocking = false;

public function __construct(RepositoryReadOnlyInterface $repository)
{
Expand Down
Loading

0 comments on commit 40b17f2

Please sign in to comment.