Skip to content

Commit

Permalink
Merge pull request #7 from bakerkretzmar/php8
Browse files Browse the repository at this point in the history
  • Loading branch information
thehappybug authored Feb 9, 2021
2 parents e71a641 + f7ebf84 commit 877fb8b
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 59 deletions.
49 changes: 49 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: Tests
on:
pull_request:
push:
branches: [master]
jobs:
test:
name: PHP ${{ matrix.php }}
runs-on: ubuntu-latest
if: "! contains(toJSON(github.event.head_commit.message), 'skip ci')"
strategy:
matrix:
php: ['5.6', '7.0', '7.1', '7.2', '7.3', '7.4', '8.0']
include:
- php: 5.6
phpunit: 5
- php: 7.0
phpunit: 6
- php: 7.1
phpunit: 6
- php: 7.2
phpunit: 7
- php: 7.3
phpunit: 7
- php: 7.4
phpunit: 7
- php: 8.0
phpunit: 8
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Set up PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
- name: Cache Composer dependencies
uses: actions/cache@v2
with:
path: ~/.composer/cache/files
key: php-${{ matrix.php }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: php-${{ matrix.php }}-composer-
- name: Install dependencies
run: composer install --prefer-source
- name: Run tests
run: |
cd tests
../vendor/bin/phpunit --coverage-text .
- name: Check code style
run: vendor/bin/phpcs --standard=PEAR src/
17 changes: 0 additions & 17 deletions .travis.yml

This file was deleted.

8 changes: 5 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
"description": "Map nested JSON structures onto PHP classes",
"license": "OSL-3.0",
"autoload": {
"psr-4": {"apimatic\\jsonmapper\\": "src/"}
"psr-4": {
"apimatic\\jsonmapper\\": "src/"
}
},
"authors": [
{
Expand All @@ -24,7 +26,7 @@
"issues": "https://github.com/apimatic/jsonmapper/issues"
},
"require-dev": {
"phpunit/phpunit": "^5 || ^6 || ^7",
"squizlabs/php_codesniffer": "^3"
"squizlabs/php_codesniffer": "^3.0.0",
"phpunit/phpunit": "^5.0.0 || ^6.0.0 || ^7.0.0 || ^8.0.0"
}
}
78 changes: 39 additions & 39 deletions src/JsonMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class JsonMapper
* This is only needed if discriminators are to be used. PHP reflection is not
* used to get child classes because most code bases use autoloaders where
* classes are lazily loaded.
*
*
* @var array
*/
public $arChildClasses = array();
Expand Down Expand Up @@ -132,7 +132,7 @@ public function map($json, $object)
= $this->arInspectedClasses[$strClassName][$key];

if ($accessor !== null) {
$providedProperties[$accessor->getName()] = true;
$providedProperties[$accessor->getName()] = true;
}

if (!$hasProperty) {
Expand Down Expand Up @@ -175,7 +175,7 @@ public function map($json, $object)

if ($isAdditional) {
if ($additionalPropertiesMethod !== null) {
$additionalPropertiesMethod->invoke($object, $key, $jvalue);
$additionalPropertiesMethod->invoke($object, $key, $jvalue);
}
continue;
}
Expand All @@ -199,13 +199,13 @@ public function map($json, $object)

/**
* Get mapped value for a property in an object.
*
*
* @param $jvalue mixed Raw normalized data for the property
* @param $type string Type of the data found by inspectProperty()
* @param $factoryMethod string Callable factory method for property
* @param $namespace string Namespace of the class
* @param $className string Name of the class
*
* @param $className string Name of the class
*
* @return mixed
*/
protected function getMappedValue(
Expand Down Expand Up @@ -316,7 +316,7 @@ public function mapClass($json, $type)
}

$ttype = ltrim($type, "\\");

if (!class_exists($type)) {
throw new \InvalidArgumentException(
'JsonMapper::mapClass() requires second argument to be a class name'
Expand All @@ -341,15 +341,15 @@ public function mapClass($json, $type)

/**
* Get class instance that best matches the class
*
*
* @param object|null $json JSON object structure from json_decode()
* @param \ReflectionClass $rc Class to get instance of. This method
* will try to first match the discriminator
* field with the discriminator value of
* the current class or its child class.
* If no matches is found, then the current
* class's instance is returned.
*
*
* @return \ReflectionClass|null Object instance if match is found.
*/
protected function getDiscriminatorMatch($json, $rc)
Expand All @@ -376,9 +376,9 @@ protected function getDiscriminatorMatch($json, $rc)

/**
* Get discriminator info
*
*
* @param \ReflectionClass $rc ReflectionClass of class to inspect
*
*
* @return array|null An array with discriminator arguments
* Element 1 is discriminator field name
* and element 2 is discriminator value.
Expand All @@ -401,9 +401,9 @@ protected function getDiscriminator($rc)

/**
* Get child classes from a ReflectionClass
*
*
* @param \ReflectionClass $rc ReflectionClass of class to inspect
*
*
* @return \ReflectionClass[] ReflectionClass instances for child classes
*/
protected function getChildClasses($rc)
Expand Down Expand Up @@ -469,7 +469,7 @@ protected function checkMissingData($providedProperties, \ReflectionClass $rc)
* Get additional properties setter method for the class.
*
* @param \ReflectionClass $rc Reflection class to check
*
*
* @return \ReflectionMethod Method or null if disabled.
*/
protected function getAdditionalPropertiesMethod(\ReflectionClass $rc)
Expand All @@ -479,23 +479,23 @@ protected function getAdditionalPropertiesMethod(\ReflectionClass $rc)
) {
$additionalPropertiesMethod = null;
try {
$additionalPropertiesMethod
$additionalPropertiesMethod
= $rc->getMethod($this->sAdditionalPropertiesCollectionMethod);
if (!$additionalPropertiesMethod->isPublic()) {
throw new \InvalidArgumentException(
$this->sAdditionalPropertiesCollectionMethod .
$this->sAdditionalPropertiesCollectionMethod .
" method is not public on the given class."
);
);
}
if ($additionalPropertiesMethod->getNumberOfParameters() < 2) {
throw new \InvalidArgumentException(
$this->sAdditionalPropertiesCollectionMethod .
$this->sAdditionalPropertiesCollectionMethod .
' method does not receive two args, $key and $value.'
);
);
}
} catch (\ReflectionException $e) {
throw new \InvalidArgumentException(
$this->sAdditionalPropertiesCollectionMethod .
$this->sAdditionalPropertiesCollectionMethod .
" method is not available on the given class."
);
}
Expand Down Expand Up @@ -546,10 +546,10 @@ public function mapArray($json, $array, $class = null)

/**
* Map an array
*
*
* @param array|null $jsonArray JSON array structure from json_decode()
* @param string $type Class name
*
*
* @return array A new array containing object of $type
* which is mapped from $jsonArray
*/
Expand Down Expand Up @@ -637,14 +637,14 @@ protected function inspectProperty(\ReflectionClass $rc, $name)

//now try to set the property directly
if ($rprop === null) {
if ($rc->hasProperty($name)
if ($rc->hasProperty($name)
&& $this->getMapAnnotation($rc->getProperty($name)) === null
) {
$rprop = $rc->getProperty($name);
} else {
//case-insensitive property matching
foreach ($rc->getProperties(\ReflectionProperty::IS_PUBLIC) as $p) {
if ((strcasecmp($p->name, $name) === 0)
if ((strcasecmp($p->name, $name) === 0)
&& $this->getMapAnnotation($p) === null
) {
$rprop = $p;
Expand Down Expand Up @@ -684,23 +684,23 @@ protected function inspectProperty(\ReflectionClass $rc, $name)

/**
* Get Phpdoc typehint for parameter
*
*
* @param \ReflectionParameter $param ReflectionParameter instance for parameter
*
*
* @return string|null
*/
protected function getParameterType(\ReflectionParameter $param)
{
if (null !== $class = $param->getClass()) {
if (PHP_VERSION_ID <= 80000 && null !== $class = $param->getClass()) {
return "\\" . $class->getName();
}

if (is_callable([$param, 'hasType']) && $param->hasType()) {
$type = $param->getType();
if ($type->isBuiltIn()) {
$typeName = static::reflectionTypeToString($type);
$typeName = static::reflectionTypeToString($type);
} else {
$typeName = "\\" + static::reflectionTypeToString($type);
$typeName = "\\" . static::reflectionTypeToString($type);
}
return $type->allowsNull() ? "$typeName|null" : $typeName;
}
Expand All @@ -710,11 +710,11 @@ protected function getParameterType(\ReflectionParameter $param)

/**
* Get name for a ReflectionType instance
*
*
* @param \ReflectionTpye $type Reflection type instance
*
*
* @return string
*
*
* @codeCoverageIgnore
*/
protected static function reflectionTypeToString(\ReflectionType $type)
Expand All @@ -730,9 +730,9 @@ protected static function reflectionTypeToString(\ReflectionType $type)

/**
* Get map annotation value for a property
*
*
* @param object $property Property of a class
*
*
* @return string|null Map annotation value
*/
protected function getMapAnnotation($property)
Expand All @@ -743,12 +743,12 @@ protected function getMapAnnotation($property)
}
return null;
}

/**
* Get map annotation value from a parsed annotation list
*
*
* @param array $annotations Parsed annotation list
*
*
* @return string|null Map annotation value
*/
protected function getMapAnnotationFromParsed($annotations)
Expand Down Expand Up @@ -947,9 +947,9 @@ protected function isFlatType($type)

/**
* Is type registered with mapper
*
*
* @param string $type Class name
*
*
* @return boolean True if registered with $this->arChildClasses
*/
protected function isRegisteredType($type)
Expand Down

0 comments on commit 877fb8b

Please sign in to comment.