liberu-genealogy / php-gedcom

Gedcom reading and writing for PHP 8.3
https://www.liberu.co.uk
MIT License
74 stars 39 forks source link

Your Example does not work #3

Closed cmette closed 6 months ago

cmette commented 7 months ago

Hello team, your example does not work

$parser = new \Gedcom\Parser();
$gedcom = $parser->parse('tmp.ged');

foreach ($gedcom->getIndi() as $individual) {
    echo $individual->getId() . ': ' . current($individual->getName())->getSurn() .
        ', ' . current($indi->$individual())->getGivn();
}

this is not a valid php code. Can you give a working example?

These lines are not executable in any PHP version:

echo $individual->getId() . ': ' . current($individual->getName())->getSurn() .
        ', ' . current($indi->$individual())->getGivn();

In my case the parser seems to work, but I can't access the decoded objects like name etc.

This part

current($individual->getName())->getSurn()

throws an exception

Call to a member function getSurn() on bool

Calling current in foreach should not make sense!

and this part

current($indi->$individual())->getGivn()

is not defined, because $indi is not defined and $indi->$individual() makes no sense.

Can you help with a few tips or provide a more detailed example?

Best regards cmette

Checklist - [X] Modify `README.md` βœ“ https://github.com/liberu-genealogy/php-gedcom/commit/8b60952e12708d5016e6bd75e6eb5e4c6a3ebefe [Edit](https://github.com/liberu-genealogy/php-gedcom/edit/sweep/your_example_does_not_work/README.md#L53-L62) - [X] Running GitHub Actions for `README.md` βœ“ [Edit](https://github.com/liberu-genealogy/php-gedcom/edit/sweep/your_example_does_not_work/README.md#L53-L62)
cmette commented 7 months ago

Try this

/* @var Indi $individual */
foreach ($gedcom->getIndi() as $individual) {
  /* @var Name $name */
  foreach ($individual->getName() as $name) {
    echo "id:{$individual->getId()} name:{$name->getName()} surn: {$name->getSurn()}";
    // better use
    dump("id:{$individual->getId()} name:{$name->getName()} surn: {$name->getSurn()}");
  }
}

and fix this in Gedcom\Record\ObjeRef\File\Form;

// add the _ before form like
protected $_form;
// add the _ before titl like
protected $_titl;
// add this line!
protected $_date;
curtisdelicata commented 7 months ago

Please send a pull request. Thank you.

sweep-ai[bot] commented 6 months ago

πŸš€ Here's the PR! #13

See Sweep's progress at the progress dashboard!
⚑ Sweep Basic Tier: I'm using GPT-4. You have 5 GPT-4 tickets left for the month and 3 for the day. (tracking ID: 8c129a9c07)

For more GPT-4 tickets, visit our payment portal. For a one week free trial, try Sweep Pro (unlimited GPT-4 tickets).

[!TIP] I can email you next time I complete a pull request if you set up your email here!


Actions (click)

GitHub Actionsβœ“

Here are the GitHub Actions logs prior to making any changes:

Sandbox logs for 85517d6
Checking README.md for syntax errors... βœ… README.md has no syntax errors! 1/1 βœ“
Checking README.md for syntax errors...
βœ… README.md has no syntax errors!

Sandbox passed on the latest main, so sandbox checks will be enabled for this issue.


Step 1: πŸ”Ž Searching

I found the following snippets in your repository. I will now analyze these snippets and come up with a plan.

Some code snippets I think are relevant in decreasing order of relevance (click to expand). If some file is missing from here, you can mention the path in the ticket description. https://github.com/liberu-genealogy/php-gedcom/blob/85517d64a2587d587abb7873fbe121019d7248a6/README.md#L50-L62 https://github.com/liberu-genealogy/php-gedcom/blob/85517d64a2587d587abb7873fbe121019d7248a6/src/Gedcom.php#L90-L205 https://github.com/liberu-genealogy/php-gedcom/blob/85517d64a2587d587abb7873fbe121019d7248a6/src/Record/Indi/Name.php#L16-L51 https://github.com/liberu-genealogy/php-gedcom/blob/85517d64a2587d587abb7873fbe121019d7248a6/tests/issue/Issue00012Test.php#L1-L23

Step 2: ⌨️ Coding

foreach ($gedcom->getIndi() as $individual) { $names = $individual->getName(); if (!empty($names)) { $name = reset($names); // Get the first name object from the array echo $individual->getId() . ': ' . $name->getSurn() . ', ' . $name->getGivn() . PHP_EOL; } }

  - This modification corrects the syntax errors and logical mistakes in the original example. It uses `reset()` instead of `current()` to get the first element of the names array, which is a safer approach since it resets the array's internal pointer to the first element.
  - The corrected example checks if the `$names` array is not empty before attempting to access it, which prevents the "Call to a member function on bool" error.
  - The variable `$indi` is removed from the example, as it was incorrectly used and is not defined anywhere in the provided code snippet.
  - This modification ensures that the example is executable and provides a clear demonstration of how to access individual names after parsing a GEDCOM file.

<pre>--- 
+++ 
@@ -57,7 +57,10 @@
 $gedcom = $parser->parse('tmp.ged');

 foreach ($gedcom->getIndi() as $individual) {
-    echo $individual->getId() . ': ' . current($individual->getName())->getSurn() .
-        ', ' . current($indi->$individual())->getGivn();
+    $names = $individual->getName();
+    if (!empty($names)) {
+        $name = reset($names); // Get the first name object from the array
+        echo $individual->getId() . ': ' . $name->getSurn() . ', ' . $name->getGivn() . PHP_EOL;
+    }
 }

Ran GitHub Actions for 8b60952e12708d5016e6bd75e6eb5e4c6a3ebefe:


Step 3: πŸ” Code Review

I have finished reviewing the code for completeness. I did not find errors for sweep/your_example_does_not_work.


πŸŽ‰ Latest improvements to Sweep:
  • New dashboard launched for real-time tracking of Sweep issues, covering all stages from search to coding.
  • Integration of OpenAI's latest Assistant API for more efficient and reliable code planning and editing, improving speed by 3x.
  • Use the GitHub issues extension for creating Sweep issues directly from your editor.

πŸ’‘ To recreate the pull request edit the issue title or description. To tweak the pull request, leave a comment on the pull request.Something wrong? Let us know.

This is an automated message generated by Sweep AI.