Skip to content

Commit 23eb7d4

Browse files
author
Ronald Diaz
authored
Throws a validation exception now if attempting to cast an object to string (#42)
* Throws a validation exception now if attempting to cast an object to string. * Removed extraneous use statement. * Added void return types for PHPUnit 8.5 compatibility. Added Throwable to StringField class. * Fixed array_key_exists() usage in Schema.php * Formatting update as per travis PHP7.1 cs check
1 parent c11e907 commit 23eb7d4

7 files changed

+13
-8
lines changed

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ php:
33
- '7.1'
44
- '7.2'
55
- '7.3'
6+
- '7.4'
67
before_script:
78
- COMPOSER_DISABLE_XDEBUG_WARN=1 composer install --prefer-dist
89
script:

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "A utility library for working with Table Schema",
44
"license": "MIT",
55
"require": {
6-
"php": ">=7.1.0 <7.4.0",
6+
"php": ">=7.1.0",
77
"justinrainbow/json-schema": "^5.2",
88
"nesbot/carbon": "^1.22",
99
"jmikola/geojson": "^1.0"

src/Fields/StringField.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace frictionlessdata\tableschema\Fields;
44

5+
use frictionlessdata\tableschema\Exceptions\FieldValidationException;
6+
57
class StringField extends BaseField
68
{
79
public function inferProperties($val, $lenient = false)
@@ -19,13 +21,13 @@ public function inferProperties($val, $lenient = false)
1921
*
2022
* @return string
2123
*
22-
* @throws \frictionlessdata\tableschema\Exceptions\FieldValidationException;
24+
* @throws FieldValidationException;
2325
*/
2426
protected function validateCastValue($val)
2527
{
2628
try {
2729
$val = (string) $val;
28-
} catch (\Exception $e) {
30+
} catch (\Throwable $e) {
2931
$val = json_encode($val);
3032
}
3133
switch ($this->format()) {

src/Schema.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,9 @@ public function fields($newFields = null)
143143
if (is_null($newFields)) {
144144
if (empty($this->fieldsCache)) {
145145
foreach ($this->descriptor()->fields as $fieldDescriptor) {
146-
if (!array_key_exists('type', $fieldDescriptor)) {
146+
if ((is_object($fieldDescriptor) && !isset($fieldDescriptor->type))
147+
|| (is_array($fieldDescriptor) && !array_key_exists('type', $fieldDescriptor))
148+
) {
147149
$field = new $this->DEFAULT_FIELD_CLASS($fieldDescriptor);
148150
} else {
149151
$field = Fields\FieldsFactory::field($fieldDescriptor);

tests/FieldTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class FieldTest extends TestCase
1717
public $DESCRIPTOR_MIN;
1818
public $DESCRIPTOR_MAX;
1919

20-
public function setUp()
20+
public function setUp(): void
2121
{
2222
$this->DESCRIPTOR_WITHOUT_TYPE = [
2323
'name' => 'id',

tests/SchemaTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class SchemaTest extends TestCase
1919
public $schemaValidSimpleFilename;
2020
public $schemaInvalidMultipleErrorsFilename;
2121

22-
public function setUp()
22+
public function setUp(): void
2323
{
2424
$this->simpleDescriptorJson = '{
2525
"fields": [
@@ -546,7 +546,7 @@ public function testSchemaInferCsvDialect()
546546
], $schema->descriptor());
547547
}
548548

549-
public function tearDown()
549+
public function tearDown(): void
550550
{
551551
foreach ($this->tempFiles as $tempFile) {
552552
if (file_exists($tempFile)) {

tests/TableTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
class TableTest extends TestCase
1616
{
17-
public function setUp()
17+
public function setUp(): void
1818
{
1919
$this->fixturesPath = dirname(__FILE__).DIRECTORY_SEPARATOR.'fixtures';
2020
$this->validSchema = new Schema((object) [

0 commit comments

Comments
 (0)