liberu-genealogy / php-dna

DNA processing and manipulating for PHP 8.3
https://www.liberu.co.uk
MIT License
31 stars 23 forks source link

Sweep: Refactor generally to improve quality the file individual.php and maintainbility plus readable by following psr standards #153

Closed curtisdelicata closed 6 months ago

curtisdelicata commented 6 months ago
Checklist - [X] Modify `src/Individual.php` ✓ https://github.com/liberu-genealogy/php-dna/commit/f3f7a9c89ca8f0e281d400921902e9139f444149 [Edit](https://github.com/liberu-genealogy/php-dna/edit/sweep/refactor_generally_to_improve_quality_th_42ca9/src/Individual.php) - [X] Modify `src/Individual.php` ✓ https://github.com/liberu-genealogy/php-dna/commit/f3f7a9c89ca8f0e281d400921902e9139f444149 [Edit](https://github.com/liberu-genealogy/php-dna/edit/sweep/refactor_generally_to_improve_quality_th_42ca9/src/Individual.php) - [X] Modify `src/Individual.php` ✓ https://github.com/liberu-genealogy/php-dna/commit/f3f7a9c89ca8f0e281d400921902e9139f444149 [Edit](https://github.com/liberu-genealogy/php-dna/edit/sweep/refactor_generally_to_improve_quality_th_42ca9/src/Individual.php) - [X] Modify `src/Individual.php` ✓ https://github.com/liberu-genealogy/php-dna/commit/f3f7a9c89ca8f0e281d400921902e9139f444149 [Edit](https://github.com/liberu-genealogy/php-dna/edit/sweep/refactor_generally_to_improve_quality_th_42ca9/src/Individual.php) - [X] Modify `src/Individual.php` ✓ https://github.com/liberu-genealogy/php-dna/commit/f3f7a9c89ca8f0e281d400921902e9139f444149 [Edit](https://github.com/liberu-genealogy/php-dna/edit/sweep/refactor_generally_to_improve_quality_th_42ca9/src/Individual.php)
sweep-ai[bot] commented 6 months ago

🚀 Here's the PR! #157

See Sweep's progress at the progress dashboard!
💎 Sweep Pro: I'm using GPT-4. You have unlimited GPT-4 tickets. (tracking ID: 6bf084ed9e)

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


Actions (click)


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-dna/blob/3d5b908204bf168b03837aa518a9e3b56b52896e/src/Individual.php#L1-L84 https://github.com/liberu-genealogy/php-dna/blob/3d5b908204bf168b03837aa518a9e3b56b52896e/src/Snps/Utils.php#L1-L196 https://github.com/liberu-genealogy/php-dna/blob/3d5b908204bf168b03837aa518a9e3b56b52896e/tests/Snps/SnpsTest.php#L1-L841

Step 2: ⌨️ Coding

private string $_name; public function __construct(string $name, mixed $raw_data = [], array $kwargs = []) { /** * Initialize an ``Individual`` object. * * Parameters * ---------- * name : str * name of the individual * raw_data : str, bytes, ``SNPs`` (or list or tuple thereof) * path(s) to file(s), bytes, or ``SNPs`` object(s) with raw genotype data * kwargs : array * parameters to ``snps.SNPs`` and/or ``snps.SNPs.merge`` */ $this->_name = $name; $init_args = $this->_get_defined_kwargs(new ReflectionMethod(SNPs::class, '__construct'), $kwargs); $merge_args = $this->_get_defined_kwargs(new ReflectionMethod(SNPs::class, ''), $kwargs); private string $name; public function __construct(string $name, mixed $rawData = [], array $kwargs = []) { /** * Initialize an ``Individual`` object. * * @param string $name Name of the individual * @param mixed $rawData Path(s) to file(s), bytes, or ``SNPs`` object(s) with raw genotype data * @param array $kwargs Parameters to ``snps.SNPs`` and/or ``snps.SNPs.merge`` */ $this->name = $name; $initArgs = $this->getDefinedKwargs(new ReflectionMethod(SNPs::class, '__construct'), $kwargs); $mergeArgs = $this->getDefinedKwargs(new ReflectionMethod(SNPs::class, 'merge'), $kwargs);

private function _get_defined_kwargs(ReflectionMethod $callable, array $kwargs): array { $parameters = $callable->getParameters(); $defined_kwargs = []; foreach ($parameters as $parameter) { $name = $parameter->getName(); if (array_key_exists($name, $kwargs)) { $defined_kwargs[$name] = $kwargs[$name]; } } return $defined_kwargs; } public function __toString(): string { return sprintf("Individual('%s')", $this->_name); } public function getName(): string { /** * Get this ``Individual``'s name. * * Returns * ------- * str */ return $this->_name; } public function getVarName(): string { return clean_str($this->_name); } /** * Get defined keyword arguments for a method * * @param ReflectionMethod $method The method to get arguments for * @param array $kwargs The keyword arguments to filter * @return array The defined keyword arguments */ private function getDefinedKwargs(ReflectionMethod $method, array $kwargs): array { $parameters = $method->getParameters(); $definedKwargs = []; foreach ($parameters as $parameter) { $name = $parameter->getName(); if (array_key_exists($name, $kwargs)) { $definedKwargs[$name] = $kwargs[$name]; } } return $definedKwargs; } /** * Get the string representation of the Individual * * @return string The string representation */ public function __toString(): string { return sprintf("Individual('%s')", $this->name); } /** * Get the Individual's name * * @return string The name */ public function getName(): string { return $this->name; } /** * Get a variable-safe version of the Individual's name * * @return string The variable-safe name */ public function getVarName(): string { return clean_str($this->name); }

public function __construct(string $name, mixed $raw_data = [], array $kwargs = []) { /** * Initialize an ``Individual`` object. * * Parameters * ---------- * name : str * name of the individual * raw_data : str, bytes, ``SNPs`` (or list or tuple thereof) * path(s) to file(s), bytes, or ``SNPs`` object(s) with raw genotype data * kwargs : array * parameters to ``snps.SNPs`` and/or ``snps.SNPs.merge`` */ $this->_name = $name; $init_args = $this->_get_defined_kwargs(new ReflectionMethod(SNPs::class, '__construct'), $kwargs); $merge_args = $this->_get_defined_kwargs(new ReflectionMethod(SNPs::class, ''), $kwargs); parent::__construct(...array_values($init_args)); if (!is_array($raw_data)) { $raw_data = [$raw_data]; } foreach ($raw_data as $file) { $s = $file instanceof SNPs ? $file : new SNPs($file, ...array_values($init_args)); $this->merge([$s], ...array_values($merge_args)); } } /** * Initialize an Individual object * * @param string $name Name of the individual * @param mixed $rawData Path(s) to file(s), bytes, or SNPs object(s) with raw genotype data * @param array $kwargs Parameters to snps.SNPs and/or snps.SNPs.merge */ public function __construct(string $name, mixed $rawData = [], array $kwargs = []) { $this->name = $name; $snpsConstructorArgs = $this->getDefinedKwargs(new ReflectionMethod(SNPs::class, '__construct'), $kwargs); $snpsMergeArgs = $this->getDefinedKwargs(new ReflectionMethod(SNPs::class, 'merge'), $kwargs); parent::__construct(...array_values($snpsConstructorArgs)); $rawDataArray = is_array($rawData) ? $rawData : [$rawData]; foreach ($rawDataArray as $data) { $snps = $data instanceof SNPs ? $data : new SNPs($data, ...array_values($snpsConstructorArgs)); $this->merge([$snps], ...array_values($snpsMergeArgs)); } }

public function __construct(string $name, mixed $raw_data = [], array $kwargs = []) { /** * Initialize an ``Individual`` object. * * Parameters * ---------- * name : str * name of the individual * raw_data : str, bytes, ``SNPs`` (or list or tuple thereof) * path(s) to file(s), bytes, or ``SNPs`` object(s) with raw genotype data * kwargs : array * parameters to ``snps.SNPs`` and/or ``snps.SNPs.merge`` */ $this->_name = $name; $init_args = $this->_get_defined_kwargs(new ReflectionMethod(SNPs::class, '__construct'), $kwargs); $merge_args = $this->_get_defined_kwargs(new ReflectionMethod(SNPs::class, ''), $kwargs); parent::__construct(...array_values($init_args)); if (!is_array($raw_data)) { $raw_data = [$raw_data]; } foreach ($raw_data as $file) { $s = $file instanceof SNPs ? $file : new SNPs($file, ...array_values($init_args)); $this->merge([$s], ...array_values($merge_args)); } } /** * Initialize an Individual object * * @param string $name Name of the individual * @param mixed $rawData Path(s) to file(s), bytes, or SNPs object(s) with raw genotype data * @param array $kwargs Parameters to snps.SNPs and/or snps.SNPs.merge */ public function __construct(string $name, mixed $rawData = [], array $kwargs = []) { $this->name = $name; $snpsConstructorArgs = $this->getDefinedKwargs(new ReflectionMethod(SNPs::class, '__construct'), $kwargs); $snpsMergeArgs = $this->getDefinedKwargs(new ReflectionMethod(SNPs::class, 'merge'), $kwargs); parent::__construct(...array_values($snpsConstructorArgs)); $rawDataArray = is_array($rawData) ? $rawData : [$rawData]; foreach ($rawDataArray as $data) { $snps = $this->createSnpsObject($data, $snpsConstructorArgs); $this->merge([$snps], ...array_values($snpsMergeArgs)); } } /** * Create an SNPs object from raw data * * @param mixed $data The raw data (file path, bytes, or SNPs object) * @param array $constructorArgs The constructor arguments for the SNPs object * @return SNPs The created SNPs object */ private function createSnpsObject(mixed $data, array $constructorArgs): SNPs { return $data instanceof SNPs ? $data : new SNPs($data, ...array_values($constructorArgs)); }

_name = $name; $init_args = $this->_get_defined_kwargs(new ReflectionMethod(SNPs::class, '__construct'), $kwargs); $merge_args = $this->_get_defined_kwargs(new ReflectionMethod(SNPs::class, ''), $kwargs); parent::__construct(...array_values($init_args)); if (!is_array($raw_data)) { $raw_data = [$raw_data]; } foreach ($raw_data as $file) { $s = $file instanceof SNPs ? $file : new SNPs($file, ...array_values($init_args)); $this->merge([$s], ...array_values($merge_args)); } } private function _get_defined_kwargs(ReflectionMethod $callable, array $kwargs): array { $parameters = $callable->getParameters(); $defined_kwargs = []; foreach ($parameters as $parameter) { $name = $parameter->getName(); if (array_key_exists($name, $kwargs)) { $defined_kwargs[$name] = $kwargs[$name]; } } return $defined_kwargs; } public function __toString(): string { return sprintf("Individual('%s')", $this->_name); } public function getName(): string { /** * Get this ``Individual``'s name. * * Returns * ------- * str */ return $this->_name; } public function getVarName(): string { return clean_str($this->_name); } } name = $name; $snpsConstructorArgs = $this->getDefinedKwargs(new ReflectionMethod(SNPs::class, '__construct'), $kwargs); $snpsMergeArgs = $this->getDefinedKwargs(new ReflectionMethod(SNPs::class, 'merge'), $kwargs); parent::__construct(...array_values($snpsConstructorArgs)); $rawDataArray = is_array($rawData) ? $rawData : [$rawData]; foreach ($rawDataArray as $data) { $snps = $this->createSnpsObject($data, $snpsConstructorArgs); $this->merge([$snps], ...array_values($snpsMergeArgs)); } } /** * Get defined keyword arguments for a method * * @param ReflectionMethod $method The method to get arguments for * @param array $kwargs The keyword arguments to filter * @return array The defined keyword arguments */ private function getDefinedKwargs(ReflectionMethod $method, array $kwargs): array { $parameters = $method->getParameters(); $definedKwargs = []; foreach ($parameters as $parameter) { $name = $parameter->getName(); if (array_key_exists($name, $kwargs)) { $definedKwargs[$name] = $kwargs[$name]; } } return $definedKwargs; } /** * Create an SNPs object from raw data * * @param mixed $data The raw data (file path, bytes, or SNPs object) * @param array $constructorArgs The constructor arguments for the SNPs object * @return SNPs The created SNPs object */ private function createSnpsObject(mixed $data, array $constructorArgs): SNPs { return $data instanceof SNPs ? $data : new SNPs($data, ...array_values($constructorArgs)); } /** * Get the string representation of the Individual * * @return string The string representation */ public function __toString(): string { return sprintf("Individual('%s')", $this->name); } /** * Get the Individual's name * * @return string The name */ public function getName(): string { return $this->name; } /** * Get a variable-safe version of the Individual's name * * @return string The variable-safe name */ public function getVarName(): string { return clean_str($this->name); } }


Step 3: 🔁 Code Review

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


🎉 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. Something wrong? Let us know.

This is an automated message generated by Sweep AI.