1

I was working on a command line application and I have this class that handles the business logic. This class has some method that loops all data from a \Generator and echo out some values.

I use \Generator because this command line application will loop a hundred of thousand of datum. and I need it to be print/echo in the command line the output on the fly

// SomeClass.php
class SomeClass {
    //... some code here
    public function someMethod($someArgs) { 
        foreach ($this->isFromAGenerator() as $data) {
            // the data above from `isFromAGenerator` 
            // is a data that is being yield
            echo $this->isAnotherMethod($data);
        }
    }
}

// index.php
$someClass = new SomeClass();
$someClass->someMethod(); 

This works fine and running smoothly but this is directly violates PSR1 2.3 Side Effects https://www.php-fig.org/psr/psr-1/#23-side-effects

How can I approach this solution without violation PSR1 side effects?

KevDev
  • 541
  • 2
  • 6
  • 20
  • 2
    Instead of echoing the data in the loop, you can push it to an array which your method then returns. That way, it's up to the caller to output it in way that fits the context. You can also reuse the same code if you need the same data but use it differently later on. – M. Eriksson May 11 '20 at 14:15
  • 1
    The aim of that PSR section is to limit individual *files* to either class declarations or procedural code (e.g. actually instantiating and calling the class method). Since `SomeClass.php` and `index.php` are separate files, I'm pretty sure you're well within the spirit of the "law", at least as far as I understand it. It would be impossible to write a class the echoed anything otherwise. – iainn May 11 '20 at 14:17
  • @MagnusEriksson ahh yes but the loop will go thru hundred of thousands of data. thats why i use generators so it will returns some value when being yielded. i will update my question thanks – KevDev May 11 '20 at 14:25
  • 1
    PSR's aren't law, They are suggestions/recommendations. If something in it makes it harder for you, change it to fit your needs. As long as you're consistent throughout your code base and it's clear what that method does (like calling it `outputFoo()`), I don't see an issue with having it "as is". – M. Eriksson May 11 '20 at 14:28

1 Answers1

0

I found an answer somewhere else (discord php server). And this is my approach I got.

// SomeClass.php

class SomeClass {
    //... some code here
    public function someMethod($someArgs, callable $callback) { 
        foreach ($this->isFromAGenerator() as $data) {
            // the data above from `isFromAGenerator` 
            // is a data that is being yield
            call_user_func($callback, $this->isAnotherMethod($data));
        }
    }
}


// index.php
$someClass = new SomeClass();
$someClass->someMethod($args, function ($data) {
    echo $data;
}); 

instead of echoing it inside SomeClass method. I just pass an callback function as a 2nd parameter and echo it out on index.php

KevDev
  • 541
  • 2
  • 6
  • 20