Skip to content

Commit

Permalink
Upgrade: php8, phpstan
Browse files Browse the repository at this point in the history
  • Loading branch information
h4kuna committed Feb 15, 2023
1 parent 68c8fdd commit 684eb80
Show file tree
Hide file tree
Showing 41 changed files with 641 additions and 918 deletions.
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
.gitattributes export-ignore
.gitignore export-ignore
.github export-ignore
changelog.md export-ignore
phpstan.neon export-ignore
tests export-ignore
48 changes: 48 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Tests

on: [push, pull_request]

jobs:
tests:
runs-on: ubuntu-latest
strategy:
matrix:
php: ['8.0', '8.1', '8.2']

fail-fast: false

name: PHP ${{ matrix.php }} tests
steps:
- uses: actions/checkout@v3
- uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: curl, simplexml, json
coverage: none

- run: composer install --no-progress --prefer-dist
- run: composer tests
- if: failure()
uses: actions/upload-artifact@v3
with:
name: output
path: tests/**/output


# code_coverage:
# name: Code Coverage
# runs-on: ubuntu-latest
# steps:
# - uses: actions/checkout@v3
# - uses: shivammathur/setup-php@v2
# with:
# php-version: 8.0
# extensions: json, mbstring, tokenizer, fileinfo
# coverage: none
#
# - run: composer install --no-progress --prefer-dist
# - run: vendor/bin/tester -p phpdbg tests -s -C --coverage ./coverage.xml --coverage-src ./src
# - run: wget https://github.com/php-coveralls/php-coveralls/releases/download/v2.4.3/php-coveralls.phar
# - env:
# COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# run: php php-coveralls.phar --verbose --config tests/.coveralls.yml
7 changes: 3 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/.idea/
coveralls.phar
/nbproject/
/vendor/
composer.lock
/tests/temp/
*~
/tests/**/output/
coverage.html
23 changes: 0 additions & 23 deletions .travis.yml

This file was deleted.

9 changes: 1 addition & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
## Data type

[![Build Status](https://travis-ci.org/h4kuna/data-type.svg?branch=master)](https://travis-ci.org/h4kuna/data-type)
[![Latest stable](https://img.shields.io/packagist/v/h4kuna/data-type.svg)](https://packagist.org/packages/h4kuna/data-type)
[![Downloads this Month](https://img.shields.io/packagist/dm/h4kuna/data-type.svg)](https://packagist.org/packages/h4kuna/data-type)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/h4kuna/data-type/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/h4kuna/data-type/?branch=master)
[![Coverage Status](https://coveralls.io/repos/github/h4kuna/data-type/badge.svg?branch=master)](https://coveralls.io/github/h4kuna/data-type?branch=master)

Previous version [v1.0.5] has object classes now are static, because PHP7 has scalar type hint.

Installation to project
Installation by composer
-----------------------
The best way to install h4kuna/data-type is using Composer:
```sh
$ composer require h4kuna/data-type
```

- [Basic](src/Basic)
- czech [Date](src/Date)
- [Immutable](src/Immutable)
- [GPS](src/Location)
- [Number](src/Number)
20 changes: 13 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
}
],
"require": {
"php": ">=7.2",
"nette/utils": ">=2.2"
"php": ">=8.0",
"nette/utils": "^4.0"
},
"autoload": {
"psr-4": {
Expand All @@ -23,16 +23,22 @@
"src/functions.php"
]
},
"autoload-dev": {
"psr-4": {
"h4kuna\\DataType\\Tests\\": "tests/src"
}
},
"require-dev": {
"salamium/testinium": "^0.1",
"phpstan/phpstan": "^1.3"
"nette/tester": "^2.4",
"phpstan/phpstan": "^1.8",
"phpstan/phpstan-strict-rules": "^1.4",
"tracy/tracy": "^2.9"
},
"config": {
"sort-packages": true
},
"scripts": {
"phpstan": [
"vendor/bin/phpstan analyse --level max src tests"
]
"phpstan": "vendor/bin/phpstan analyse",
"tests": "vendor/bin/tester -s -j 4 --colors 1 -s -C tests/src"
}
}
56 changes: 0 additions & 56 deletions phpstan-baseline.neon

This file was deleted.

8 changes: 7 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
parameters:
level: max
paths:
- src
- tests/src

includes:
- phpstan-baseline.neon
- vendor/phpstan/phpstan-strict-rules/rules.neon
80 changes: 44 additions & 36 deletions src/Basic/Arrays.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,13 @@
final class Arrays
{

private function __construct()
{
}


/**
* Better array_combine where values array does not need same size.
* @param array<string|int> $keys
* @param array<mixed> $values
* @param mixed $value
* @return array<string|int, mixed>
* @throws \h4kuna\DataType\Exceptions\InvalidArgumentsException
*/
public static function combine(array $keys, array $values, $value = null)
public static function combine(array $keys, array $values, mixed $value = null): array
{
$diff = count($keys) - count($values);

Expand All @@ -30,48 +23,28 @@ public static function combine(array $keys, array $values, $value = null)
throw new DataType\Exceptions\InvalidArgumentsException('Array of values can\'t be bigger than keys.');
}

$result = array_combine($keys, $values);
assert($result !== false);

return $result;
return array_combine($keys, $values);
}


/**
* Implode only values where strlen > 0 and you can define keys.
* @param array<string|int, scalar> $array
* @param string|int $keys
* @return string
* @param array<string|int, scalar|null> $array
*/
public static function concatWs(string $glue, $array, ...$keys)
public static function concatWs(string $glue, array $array): string
{
$out = '';
foreach ($keys ? self::intersectKeys($array, $keys) : $array as $value) {
if (strlen((string) $value) === 0) {
continue;
} elseif ($out !== '') {
$out .= $glue;
}
$out .= $value;
}

return $out;
return implode($glue, array_filter($array, fn ($v): bool => $v !== false && $v !== '' && $v !== null));
}


/**
* COALESCE similar behavior database.
* @param array<string|int, mixed> $array
* @param string|int $keys
* @return mixed
* @param iterable<string|int, mixed> $array
*/
public static function coalesce($array, ...$keys)
public static function coalesce(iterable $array): mixed
{
if ($keys !== []) {
$array = self::intersectKeys($array, $keys);
}
foreach ($array as $v) {
if ($v !== null && $v !== false && $v !== '') {
if ($v !== null) {
return $v;
}
}
Expand All @@ -81,12 +54,24 @@ public static function coalesce($array, ...$keys)


/**
* Unset keys from array.
* @deprecated use unsetKeys()
* @param array<mixed> $array
* @param string|int $keys
* @return array<mixed>
*/
public static function keysUnset(&$array, ...$keys): array
{
return self::unsetKeys($array, ...$keys);
}


/**
* Unset keys from array.
* @param array<mixed> $array
* @param string|int $keys
* @return array<mixed>
*/
public static function unsetKeys(&$array, ...$keys): array
{
$out = [];
foreach ($keys as $key) {
Expand All @@ -111,4 +96,27 @@ public static function intersectKeys(array $values, array $keys): array
return array_intersect_key($values, array_flip($keys));
}


/**
* @return array<int>
*/
public static function generateNumbers(int $from, int $to): array
{
$values = range($from, $to, ($from < $to) ? 1 : -1);

return array_combine($values, $values);
}


/**
* @param array<mixed> $array1
* @param array<mixed> $array2
* @param array<mixed> ...$arrays
* @return array<mixed>
*/
public static function mergeUnique(array $array1, array $array2, array ...$arrays): array
{
return array_values(array_unique(array_merge($array1, $array2, ...$arrays)));
}

}
31 changes: 31 additions & 0 deletions src/Basic/BitwiseOperations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php declare(strict_types=1);

namespace h4kuna\DataType\Basic;

class BitwiseOperations
{

public static function check(int $number, int $flag): bool
{
return ($number & $flag) !== 0;
}


public static function checkStrict(int $number, int $flag): bool
{
return ($number & $flag) === $number;
}


public static function add(int &$number, int $flag): void
{
$number |= $flag;
}


public static function remove(int &$number, int $flag): void
{
$number &= ~$flag;
}

}
Loading

0 comments on commit 684eb80

Please sign in to comment.