Skip to content

Error when $person->links is null #48

Closed
@nono303

Description

Problem description

  1. Library version or last commit you have 1c1b4076
  2. PHP version you use 8.0.0
  3. Detailed problem description phpGPX\Parsers\PersonParser.php:48;"Invalid argument supplied for foreach()"

If you think that problem is connected with your file content, please attach it to the issue.

Code snippet you use

			$gpx = new phpGPX();
			$gpx_file = $gpx->load($filein);
....
			$p = new Person();
			$p->name = "blabla";
			$gpx_file->metadata->author = $p;
.....
			$out = $gpx_file->toXML()->saveXML();

Expected behaviour

script continue even if $person->links is null

Possible solution

diff --git "a/src/phpGPX/Parsers/PersonParser.php" "b/src/phpGPX/Parsers/PersonParser.php"
index 8795bc8..227b864 100644
--- "a/src/phpGPX/Parsers/PersonParser.php"
+++ "b/src/phpGPX/Parsers/PersonParser.php"
@@ -45,9 +45,11 @@ abstract class PersonParser
 			$node->appendChild($child);
 		}
 
-		foreach ($person->links as $link) {
-			$child = LinkParser::toXML($link, $document);
-			$node->appendChild($child);
+		if(!is_null($person->links)){
+			foreach ($person->links as $link) {
+				$child = LinkParser::toXML($link, $document);
+				$node->appendChild($child);
+			}
 		}
 
 		return $node;

and may the !is_null() test must be done somewhere else

Something interesting or funny

#confinedshortreading https://craphound.com/overclocked/Cory_Doctorow_-_Overclocked_-_When_Sysadmins_Ruled_the_Earth.html

Checklist

Put x into brackets if true.

[X] PHP version I use
[X] Package version I use

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions