nozavroni / csvelte

🕺🏻 CSV and Tabular Data library for PHP
http://phpcsv.com/
Other
6 stars 0 forks source link

Refactor CSVelte\Collection -- it's too complex #148

Closed nozavroni closed 7 years ago

nozavroni commented 7 years ago

As is reported in Scrutinizer's inspection of the Collection class, it is too complex. It needs to be refactored and most likely broken down into two or more classes. I think the most logical way to do this would be to, at the very least, create a class for single-dimensional collections and another for multi-dimensional ones. I think it's probably also a good idea to create separate classes for standard multi-dimensional collections and tabular multi-dimensional collections. So, with that in mind ,the new class structure should be something like...

Also create a StringCollection class that does exactly the same things as the regular one-dimensional collection except that it expects (and returns) a string. It would work something like...

<?php
$str = new StringCollection("foobar");
$str->map(function($char){
    if ($char == "o") return 0;
    return strtoupper($char);
});
echo $str; // prints F00BAR
/CSVelte
    /Collection
        /Multi
            *MixedCollection.php
            MultiCollection.php
            TabularCollection.php
        Abstract.php 
        Collection.php
        StringCollection.php

*Not sure if this one will be necessary or not but it would be for collections that contain both values AND arrays

So the main class would be CSVelte\Collection\Abstract. CSVelte\Collection\Collection would extend that and would be for single-dimensional collections. Then CSVelte\Collection\StringCollection and CSVelte\Collection\MultiCollection will extend that. CSVelte\Collection\TabularCollection would extend CSVelte\Collection\MultiCollection.

The collect factory would inspect its input and return the appropriate Collection class.

<?php
$str = collect("foobar"); // would return StringCollection 
$coll = collect([1,2,3,4,5]); // would return Collection
$multi = collect([
    [5,10,15,20,25],
    ["foo","bar","baz"],
    ["I","Like","Arrays"]
]); // would return MultiCollection
$multi = collect(
    [5,10,15,20,25],
    ["foo","bar","baz"],
    ["I","Like","Arrays"]
); // would also return MultiCollection (note the lack of array brackets)
$mixed = collect(
    [5,10,15,20,25],
    "foo",
    "bar",
    "baz",
    ["I","Like","Arrays"]
); // would return MixedCollection (if I end up implementing it)
$table = collect([
    ["first" => "Luke", "last" => "Visinoni", "middle" => "Daniel"],
    ["first" => "Margaret", "last" => "Kelly", "middle" => "Elizabeth"],
    ["first" => "Lorrie", "last" => "Visinoni", "middle" => "Lynn"],
]); // would return TabularCollection
nozavroni commented 7 years ago

This is a duplicate... I already have this issue, see #141.