The question sounds retrograde but I have about 50 variables being passed to a class. Basically an array of data I call 'filters' that is first passed to a prepFiltersMethod($filters). This prep filters method does some things sets some more filters needed in other methods for queries. It returns a larger subset of data of $filters after getting some additional things etc..

EDIT: This is HOW to control data type of question, I tried to clean up the question a bit, in short I'm asking how to handle a large input array into a class. if Junk data is passed along into it or deficient data, should we try to manage it. What I'm asking is if you have a big array coming into a class, would you take the extra step to write it into a new array to ensure it is the RIGHT data etc. is used for control purposes ? Weed out keys that shouldn't end up in it etc.? Say, take $filters array, pick out the keys we wanted and write it into a new array, and then use that ? In short I'm wondering if it would be helpful to use a builder to do this or how other people would handle. If we do nothing it works fine but may be a risk later down the road?

OVERVIEW:

  • Say we have a multi dimensional array called $filters.
  • $filters is being passed to a class (contains the details of a user request).
  • The $filters class goes through a prepare method that adds a few more items to this $filters array.
  • Now that the filters are prepared and checked, it will be used to build query and return results from another method. $filters is passed to getInventory method that looks at the filters and builds out a query and returns data from the DB.

QUESTION:

As it stands, $filters array is being passed around growing and shrinking as needed. Anything can be passed to the class with no gatekeeping, Should this be rectified? In other words, the method that first prepares the $filters array, should it copy the variables into a new array (e.g. build a new array) ?

Someone passes in :

$filters['x']
$filters['y']
$filters['z']
... ... ...

fine enough those are all needed/usable, but what if they pass in

$filters['x']
$filters['a']
$filters['b']
... ... ...

where 'a' and 'b' are not needed ?

Do we care about this ? should I employ something that ensures that the input data is more complete?

1) builder pattern? 2) create a new array and re-write them ? 3) Not worry about, let the other methods ignore data it doesn't need? 4) A standard class that can get and set 5) A global array in the class $this->filters (hate this idea) 6) Other ideas ?

有帮助吗?

解决方案

If I got your right, you have a bunch of unstructured data to be passed around, and since you now see this will cause issues, you try to give it some local structure just for calling one method. Of course, this approach may not be worth the effort.

I think the only sensible solution to this is to add more structure to your data not just for prepFilters. Make your $filters a properly structured DTO or class, maybe hierarchical structured, right from the start. An array or dictionary is not the best data structure for passing around ~50 different values of different type and purpose around, dissecting it into a handful of small classes can make your code much more maintainable.

The attributes need proper names, and you should get an error by the compiler or at least from the run time system when you introduce a typo into an attribute name and try to access a non-existing attribute.

That way, your data structures stay extensible, and you don't have to think about in which order the data comes in or will be returned. It will become much simpler to pass specific parts of the data around in your program, and prepFilters will probably not be the only function which will benefit from this.

许可以下: CC-BY-SA归因
scroll top