Skip to content

Commit

Permalink
Strict types=1 (#12)
Browse files Browse the repository at this point in the history
* strict_types=1
* PHP 8+ in Travis-CI as experimental builds
* Full Qualified Usage std functions/classes/consts
* Finalize classes
  • Loading branch information
SmetDenis committed Apr 21, 2021
1 parent aac87dc commit 7c1ae24
Show file tree
Hide file tree
Showing 28 changed files with 138 additions and 77 deletions.
9 changes: 7 additions & 2 deletions .phan/config.php
Expand Up @@ -11,12 +11,13 @@
* @license MIT
* @copyright Copyright (C) JBZoo.com, All rights reserved.
* @link https://github.com/JBZoo/Data
* @author Denis Smetannikov <denis@jbzoo.com>
*/

declare(strict_types=1);

$default = include __DIR__ . '/../vendor/jbzoo/codestyle/src/phan/default.php';

return array_merge($default, [
$config = array_merge($default, [
'file_list' => [
'src/functions.php'
],
Expand All @@ -30,3 +31,7 @@
'vendor/symfony/yaml',
]
]);

$config['plugins'][] = 'NotFullyQualifiedUsagePlugin';

return $config;
8 changes: 8 additions & 0 deletions .travis.yml
Expand Up @@ -19,6 +19,14 @@ php:
- 7.2
- 7.3
- 7.4
- 8.0
- nightly

jobs:
fast_finish: true
allow_failures:
- php: 8.0
- php: nightly

env:
matrix:
Expand Down
2 changes: 1 addition & 1 deletion README.md
@@ -1,6 +1,6 @@
# JBZoo / Data

[![Build Status](https://travis-ci.org/JBZoo/Data.svg)](https://travis-ci.org/JBZoo/Data) [![Coverage Status](https://coveralls.io/repos/JBZoo/Data/badge.svg)](https://coveralls.io/github/JBZoo/Data) [![Psalm Coverage](https://shepherd.dev/github/JBZoo/Data/coverage.svg)](https://shepherd.dev/github/JBZoo/Data)
[![Build Status](https://travis-ci.org/JBZoo/Data.svg?branch=master)](https://travis-ci.org/JBZoo/Data) [![Coverage Status](https://coveralls.io/repos/JBZoo/Data/badge.svg)](https://coveralls.io/github/JBZoo/Data) [![Psalm Coverage](https://shepherd.dev/github/JBZoo/Data/coverage.svg)](https://shepherd.dev/github/JBZoo/Data) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/jbzoo/data/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/jbzoo/data/?branch=master) [![CodeFactor](https://www.codefactor.io/repository/github/jbzoo/data/badge)](https://www.codefactor.io/repository/github/jbzoo/data/issues) [![PHP Strict Types](https://img.shields.io/badge/strict__types-%3D1-brightgreen)](https://www.php.net/manual/en/language.types.declarations.php#language.types.declarations.strict)
[![Stable Version](https://poser.pugx.org/jbzoo/data/version)](https://packagist.org/packages/jbzoo/data) [![Latest Unstable Version](https://poser.pugx.org/jbzoo/data/v/unstable)](https://packagist.org/packages/jbzoo/data) [![Dependents](https://poser.pugx.org/jbzoo/data/dependents)](https://packagist.org/packages/jbzoo/data/dependents?order_by=downloads) [![GitHub Issues](https://img.shields.io/github/issues/jbzoo/data)](https://github.com/JBZoo/Data/issues) [![Total Downloads](https://poser.pugx.org/jbzoo/data/downloads)](https://packagist.org/packages/jbzoo/data/stats) [![GitHub License](https://img.shields.io/github/license/jbzoo/data)](https://github.com/JBZoo/Data/blob/master/LICENSE)


Expand Down
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -17,7 +17,7 @@
},

"require-dev" : {
"jbzoo/toolbox-dev" : "^2.9.0",
"jbzoo/toolbox-dev" : "^2.9.2",
"jbzoo/utils" : "^4.2.3",
"symfony/yaml" : "^5.2.2"
},
Expand Down
45 changes: 22 additions & 23 deletions src/Data.php
Expand Up @@ -11,15 +11,14 @@
* @license MIT
* @copyright Copyright (C) JBZoo.com, All rights reserved.
* @link https://github.com/JBZoo/Data
* @author Denis Smetannikov <denis@jbzoo.com>
*/

declare(strict_types=1);

namespace JBZoo\Data;

use ArrayObject;
use JBZoo\Utils\Filter;
use RecursiveArrayIterator;
use RecursiveIteratorIterator;

use function JBZoo\Utils\bool;
use function JBZoo\Utils\float;
Expand All @@ -41,11 +40,11 @@ public function __construct($data = [])
{
$this->setFlags(ArrayObject::ARRAY_AS_PROPS);

if ($data && is_string($data) && file_exists($data)) {
if ($data && \is_string($data) && \file_exists($data)) {
$data = self::readFile($data);
}

if (is_string($data)) {
if (\is_string($data)) {
$data = $this->decode($data);
}

Expand All @@ -60,17 +59,17 @@ public function __construct($data = [])
protected function decode(string $string)
{
/** @noinspection UnserializeExploitsInspection */
return unserialize($string, []);
return \unserialize($string, []);
}

/**
* Utility Method to serialize the given data
* @param mixed $data The data to serialize
* @return string The serialized data
*/
protected function encode($data)
protected function encode($data): string
{
return serialize($data);
return \serialize($data);
}

/**
Expand Down Expand Up @@ -168,18 +167,18 @@ public function find(string $key, $default = null, $filter = null, string $separ
}

// explode search key and init search data
$parts = (array)explode($separator, $key);
$parts = (array)\explode($separator, $key);
$data = $this;

foreach ($parts as $part) {
// handle ArrayObject and Array
if (($data instanceof ArrayObject || is_array($data)) && isset($data[$part])) {
if (($data instanceof ArrayObject || \is_array($data)) && isset($data[$part])) {
$data = $data[$part];
continue;
}

// handle object
if (is_object($data) && isset($data->$part)) {
if (\is_object($data) && isset($data->$part)) {
$data = &$data->$part;
continue;
}
Expand Down Expand Up @@ -214,8 +213,8 @@ protected static function filter($value, $filter)
*/
public function search($needle)
{
$aIterator = new RecursiveArrayIterator($this->getArrayCopy());
$iterator = new RecursiveIteratorIterator($aIterator);
$aIterator = new \RecursiveArrayIterator($this->getArrayCopy());
$iterator = new \RecursiveIteratorIterator($aIterator);

while ($iterator->valid()) {
$iterator->current();
Expand All @@ -238,7 +237,7 @@ public function flattenRecursive(): array
{
$flat = [];

foreach (new RecursiveIteratorIterator(new RecursiveArrayIterator($this->getArrayCopy())) as $value) {
foreach (new \RecursiveIteratorIterator(new \RecursiveArrayIterator($this->getArrayCopy())) as $value) {
$flat[] = $value;
}

Expand All @@ -253,8 +252,8 @@ protected static function readFile(string $filePath)
{
$contents = false;

if ($realPath = realpath($filePath)) {
$contents = file_get_contents($realPath);
if ($realPath = \realpath($filePath)) {
$contents = \file_get_contents($realPath);
}

return $contents;
Expand All @@ -267,21 +266,21 @@ protected static function readFile(string $filePath)
*/
protected static function isMulti(array $array): bool
{
$arrayCount = array_filter($array, '\is_array');
return count($arrayCount) > 0;
$arrayCount = \array_filter($array, '\is_array');
return \count($arrayCount) > 0;
}

/**
* @param mixed $index
* @param int|string $key
* @return mixed|null
*/
public function offsetGet($index)
public function offsetGet($key)
{
if (!property_exists($this, $index)) {
if (!\property_exists($this, (string)$key)) {
return null;
}

return parent::offsetGet($index);
return parent::offsetGet($key);
}

/**
Expand All @@ -296,7 +295,7 @@ public function offsetGet($index)
*/
public function is(string $key, $compareWith = true, bool $strictMode = false): bool
{
if (strpos($key, '.') === false) {
if (\strpos($key, '.') === false) {
$value = $this->get($key);
} else {
$value = $this->find($key);
Expand Down
3 changes: 2 additions & 1 deletion src/Exception.php
Expand Up @@ -11,9 +11,10 @@
* @license MIT
* @copyright Copyright (C) JBZoo.com, All rights reserved.
* @link https://github.com/JBZoo/Data
* @author Denis Smetannikov <denis@jbzoo.com>
*/

declare(strict_types=1);

namespace JBZoo\Data;

/**
Expand Down
21 changes: 11 additions & 10 deletions src/Ini.php
Expand Up @@ -11,17 +11,18 @@
* @license MIT
* @copyright Copyright (C) JBZoo.com, All rights reserved.
* @link https://github.com/JBZoo/Data
* @author Denis Smetannikov <denis@jbzoo.com>
*/

declare(strict_types=1);

namespace JBZoo\Data;

/**
* Class Ini
*
* @package JBZoo\Data
*/
class Ini extends Data
final class Ini extends Data
{
/**
* Utility Method to unserialize the given data
Expand All @@ -31,14 +32,14 @@ class Ini extends Data
*/
protected function decode(string $string)
{
return parse_ini_string($string, true, INI_SCANNER_NORMAL);
return \parse_ini_string($string, true, \INI_SCANNER_NORMAL);
}

/**
* @param mixed $data
* @return string
*/
protected function encode($data)
protected function encode($data): string
{
return $this->render($data, []);
}
Expand All @@ -52,22 +53,22 @@ protected function render(array $data = [], array $parent = []): string
{
$result = [];
foreach ($data as $dataKey => $dataValue) {
if (is_array($dataValue)) {
if (\is_array($dataValue)) {
if (self::isMulti($dataValue)) {
$sections = array_merge($parent, (array)$dataKey);
$sections = \array_merge($parent, (array)$dataKey);
$result[] = '';
$result[] = '[' . implode('.', $sections) . ']';
$result[] = '[' . \implode('.', $sections) . ']';
$result[] = $this->render($dataValue, $sections);
} else {
foreach ($dataValue as $key => $value) {
$result[] = $dataKey . '[' . $key . '] = "' . str_replace('"', '\"', $value) . '"';
$result[] = $dataKey . '[' . $key . '] = "' . \str_replace('"', '\"', $value) . '"';
}
}
} else {
$result[] = $dataKey . ' = "' . str_replace('"', '\"', $dataValue) . '"';
$result[] = $dataKey . ' = "' . \str_replace('"', '\"', $dataValue) . '"';
}
}

return implode(Data::LE, $result);
return \implode(Data::LE, $result);
}
}
9 changes: 5 additions & 4 deletions src/JSON.php
Expand Up @@ -11,17 +11,18 @@
* @license MIT
* @copyright Copyright (C) JBZoo.com, All rights reserved.
* @link https://github.com/JBZoo/Data
* @author Denis Smetannikov <denis@jbzoo.com>
*/

declare(strict_types=1);

namespace JBZoo\Data;

/**
* Class JSON
*
* @package JBZoo\Data
*/
class JSON extends Data
final class JSON extends Data
{
/**
* Utility Method to unserialize the given data
Expand All @@ -31,7 +32,7 @@ class JSON extends Data
*/
protected function decode(string $string)
{
return json_decode($string, true, 512, JSON_BIGINT_AS_STRING);
return \json_decode($string, true, 512, \JSON_BIGINT_AS_STRING);
}

/**
Expand All @@ -42,6 +43,6 @@ protected function decode(string $string)
*/
protected function encode($data): string
{
return (string)json_encode($data, JSON_PRETTY_PRINT | JSON_BIGINT_AS_STRING);
return (string)\json_encode($data, \JSON_PRETTY_PRINT | \JSON_BIGINT_AS_STRING);
}
}
15 changes: 8 additions & 7 deletions src/PhpArray.php
Expand Up @@ -11,16 +11,17 @@
* @license MIT
* @copyright Copyright (C) JBZoo.com, All rights reserved.
* @link https://github.com/JBZoo/Data
* @author Denis Smetannikov <denis@jbzoo.com>
*/

declare(strict_types=1);

namespace JBZoo\Data;

/**
* Class PhpArray
* @package JBZoo\Data
*/
class PhpArray extends Data
final class PhpArray extends Data
{
/**
* Class constructor
Expand All @@ -29,7 +30,7 @@ class PhpArray extends Data
*/
public function __construct($data = [])
{
if ($data && is_string($data) && file_exists($data)) {
if ($data && \is_string($data) && \file_exists($data)) {
$data = $this->decode($data);
}

Expand All @@ -44,7 +45,7 @@ public function __construct($data = [])
*/
protected function decode(string $string)
{
if (file_exists($string)) {
if (\file_exists($string)) {
return include $string;
}
}
Expand All @@ -55,14 +56,14 @@ protected function decode(string $string)
* @param mixed $data The data to serialize
* @return string The serialized data
*/
protected function encode($data)
protected function encode($data): string
{
$data = [
'<?php',
'',
'return ' . var_export($data, true) . ';',
'return ' . \var_export($data, true) . ';',
];

return implode(self::LE, $data);
return \implode(self::LE, $data);
}
}

0 comments on commit 7c1ae24

Please sign in to comment.