This method takes search a search keyword and parsed mysql query, and rewrites the where expression to include LIKE %keyword%.
It works well, but I dont know if its good or bad practice to have a method with this many loops...
private function build_where($query_array, $options)
{
//add WHERE starting point
$where = '';
if(!empty($query_array['WHERE']))
{
//build where array
$where_array = $query_array['WHERE'];
//start the where
$where .= 'WHERE ';
//get columns array
$columns_array = $this->build_columns_array($query_array);
//if there is a search string
if(!empty($options['sSearch']))
{
//check for enabled columns
$i = 0;
$columns_length = count($columns_array);
for($i; $i < intval($columns_length); $i++)
{
//create the options boolean array
$searchable_columns['bSearchable_'.$i] = $options['bSearchable_'.$i];
}
//loop through searchable_columns for true values
foreach($searchable_columns as $searchable_column_key => $searchable_column_val)
{
if($searchable_column_val == true)
{
//get an integer from the searchable_column key
$column_id = preg_replace("/[^0-9]/", '', $searchable_column_key);
//lookup column name by index
foreach($columns_array as $columns_array_key => $columns_array_val)
{
//if the $columns_array_key matches the $column_id
if($columns_array_key == $column_id)
{
//loop to build where foreach base expression
$i = 0;
$where_length = count($where_array);
for($i; $i < intval($where_length); $i++)
{
//append the existing WHERE Expressions
$where .= $where_array[$i]['base_expr'];
}
//append the LIKE '%$options['sSearch'])%'
$where .= ' AND '.$columns_array_val." LIKE '%".$options['sSearch']."%' OR ";
}
}
}
}
//remove the last OR
$where = substr_replace($where, "", -3);
}
else
{
//loop to build where
$i = 0;
$where_length = count($where_array);
for($i; $i < intval($where_length); $i++)
{
$where .= $where_array[$i]['base_expr'];
}
}
}
//print_r($where_length);
return $where;
}
The school of thought of Kent Beck or Martin Fowler would actually advise you to refactor this large methods down to many small methods. It's not easily read in my opinion, which would be the main reason to refactor.
Breaking up methods is not primarily about reuse. Doing so can make code easier to read, test, and maintain. Clear method names can also substitute for inline comments. This method does two high-level things which could be separated: building a where clause with options and without options. Another hint for me is that the logic that builds the where clause with options looks meaty enough to warrant its own method.
private function build_where($query_array, $options) {
if(!empty($query_array['WHERE'])) {
$where_array = $query_array['WHERE'];
$columns_array = $this->build_columns_array($query_array);
if (empty($options['sSearch'])) {
return $this->build_where_with_options($where_array, $columns_array, $options);
}
else {
return $this->build_where_without_options($where_array, $columns_array);
}
}
else {
return '';
}
}
Now you can quickly scan build_where() to see that there are three possible forms the where clause may take and when along with the input each form needs to produce its result.
Here are some minor improvements you can make throughout your code:
count() returns an integer and doesn't need the intval() calls in your for loops. Even if you left those in, it would be better to apply the call outside the loop so its done only once as it yields the same value each time.
if($searchable_column_val == true) is equivalent to if($searchable_column_val) since both cast $searchable_column_val to a boolean value and the latter passes when that casted boolean value equals true.
$where = substr_replace($where, "", -3) can be replace with $where = substr($where, 0, -3) and is a little clearer.
Instead of looping through an array looking for a specific key you can take advantage of PHP's arrays by simply grabbing the value with that key.
For the last one, this code
foreach($columns_array as $columns_array_key => $columns_array_val)
{
//if the $columns_array_key matches the $column_id
if($columns_array_key == $column_id)
{ ... }
}
can be replaced by this
$columns_array_val = $columns_array[$column_id];
...
Personal preference really. Some programmers would chop this up into several functions. Personally, I think it's fine the way you have it. If I saw something that I thought might be reusable, I'd refactor it out into a separate file that could be included.
In my opinion, some programmers are too quick to make things "reuesable" before they even have something to reuse it with.
Related
i have some condition if request isset or no and retun in view blade laravel like this:
$compare1 = $request->compare1;
$compare2 = $request->compare2;
if (isset($compare1)) {
$laptop1 = Laptop::where('slug', $compare1)->firstOrFail();
return view('compare.index', ['laptop1' => $laptop1->id]);
} elseif(isset($compare2)) {
$laptop2 = Laptop::where('slug', $compare2)->firstOrFail();
return view('compare.index', ['laptop1' => $laptop1->id, 'laptop2' => $laptop2->id]);
}elseif(isset($compare1, $compare)) {
$laptop1 = Laptop::where('slug', $compare1)->firstOrFail();
$laptop2 = Laptop::where('slug', $compare2)->firstOrFail();
return view('compare.index', ['laptop1' => $laptop1->id, 'laptop2' => $laptop2->id]);
}else {
return view('compare.index');
}
if isset($compare1, $compare) run, $laptop2 not found, any solution for this case...?
Thanks before
The structure you have is incorrect. Currently, your code elseif(isset($compare1, $compare)) will never execute because if either $compare1 or $compare2 are set, your if statement will already exit before it gets to the 3rd one. You also have a lot of redundant code (repeating a line of code depending on which if block is executed) which is easily reduced to fewer lines and cleaner code.
Simple Approach
Consider this; (You should be able to replace your entire if block with this)
//set up an empty array to return
$return = [];
//check if `$compare1` is set, and add to return array if it is
if(isset($compare1)) {
$return['laptop1'] = (Laptop::where('slug', $compare1)->firstOrFail())->id;
}
//same as above but for `$compare2`
if(isset($compare2)) {
$return['laptop2'] = (Laptop::where('slug', $compare2)->firstOrFail())->id;
}
return view('compare.index', $return);
Dynamic Approach
This may be a little bit overkill if you are just doing 2 comparisons but it definitely has some upsides.
Similar amount of code as the simple approach
No need to define separate variables for every comparison (e.g $compare1 = $request->compare1;, etc)
Easily add more comparisons to your return by simply adding them to the $comparisons array
Future Proof
Code:
//empty array to return
$return = [];
//list of variables to compare
$comparisons = ['compare1', 'compare2'];
//loop through each comparison
foreach($comparisons as $key => $request_object) {
$count = $key + 1; //keys start at 0, so we add 1 to make it count sequentially 1,2,3 ...
$comparison = $request->{$request_object}; //grab your comparison object
//check if comparison object is set, add it to return array if it is
if(isset($comparison)) {
$return["laptop{$count}"] = (Laptop::where('slug', $comparison)->firstOrFail())->id;
}
}
return view('compare.index', $return);
Maybe it should be better to split code in parts to improve code quality.
Some thoughts after analyze your code:
Always returns view('compare.index')
Laravel Request contains a has method
Multiple conditional concatenation using if elseif.. (it decrease legibility)
It's more logical to pass the complete model to view instead of only id.
Otherwise, rename var to laptop1Id or similar
Take care of code duplication
Proposal
if($request->has('compare1')) {
$laptop1 = Laptop::where('slug', $request->get('compare1'))->firstOrFail();
}
if($request->has('compare2')) {
$laptop2 = Laptop::where('slug', $request->get('compare2'))->firstOrFail();
}
return view('compare.index', compact('laptop1', 'laptop2'));
If find/search a Laptop is a domain rule, you could encapsulate it in a method/scope in model, querying like:
$laptop = Laptop::findOrFailBySlug($val);
$laptop = Laptop::slug($val)->firstOrFail();
The typical algorithm of search until found, in PHP and for some arrays where we can't use the language array search functions (I think), could be implemented in this way:
$found = false;
while (list($key, $alreadyRP) = each($this->relatedProducts) && !$found) {
if ($alreadyRP->getProduct()->getId() === $rp->getProduct()->getId()) {
$found = true;
}
}
if (!$found) {
// Do something here
}
Please, take it as pseudocode, I didn't executed it. What I like about it, is that it gracefully stops if it is found what we are looking for.
Now, due to the "each" function is deprecated, I have to code something like this:
$found = false;
foreach ($this->relatedProducts as $alreadyRP) {
if ($alreadyRP->getProduct()->getId() === $rp->getProduct()->getId()) {
$found = true;
break;
}
}
if (!$found) {
// Do something here
}
To put a "break" statement inside a "for" loop is ugly in structured programming. Yes, it is optional, but if we avoid it, the "foreach" will go through all the array, which is not the most efficient way.
Any idea to recover the efficiency and structure that "each" gives in this case?
Thank you.
The beauty of the each() method is in the eye of the beholder, but there are other reasons to prefer foreach, including this interesting bit of information from the RFC that led to the deprecation of each()
The each() function is inferior to foreach in pretty much every imaginable way, including being more than 10 times slower.
If the purpose of the method is to // Do something here if the $rp is not found in $this->relatedProducts, I think a more "beautiful" way to handle it would be to extract the search through related products into a separate method.
protected function inRelatedProducts($id) {
foreach ($this->relatedProducts as $alreadyRP) {
if ($alreadyRP->getProduct()->getId() === $id) {
return true;
}
}
return false;
}
Moving the related products search into a separate method is advantageous because
It separates that functionality from the original method so that it becomes reusable instead of being tied to whatever // Do something here does.
It simplifies the original method so it can focus on its main task
$id = $rp->getProduct()->getId();
if (!$this->inRelatedProducts($id)) {
// Do something here
}
It simplifies the search code because if it's contained in its own method, you can just return true; as soon as you find a match, so you won't need to break, or to keep track of a $found variable at all.
On the other hand, if this was my project I would be looking for a way to remove the need for this method by populating $this->relatedProducts so that it's indexed by ID (assuming ID is unique there) so the determination could be reduced to
$id = $rp->getProduct()->getId();
if (isset($this->relatedProducts[$id])) { ...
If you're looking for a rewrite that doesn't involve extra variables, you can replace the each call with calls to current and next:
do {
$found = (current($this->relatedProducts)->getProduct()->getId() === $rp->getProduct()->getId());
} while (empty($found) && false !== next($array));
This is a mechanical translation, and it relies merely on the definition of each (emphasis mine):
Return the current key and value pair from an array and advance the array cursor
It also suffers the same deficiency of the original each version: it doesn't handle empty arrays.
That said, please don't use each, or any of its siblings, for new code. This from a guy who voted "No" on the RFC! Why? Because the performance sucks:
1017$ cat trial.php
<?php
$array = range(0, 999);
$begin = -microtime(true);
for ($i = 0; $i < 10000; $i++) {
reset($array);
$target = rand(333, 666);
do {
$found = (current($array) === $target);
} while (empty($found) && false !== next($array));
}
printf("%.5f\n", $begin + microtime(true));
$begin = -microtime(true);
for ($i = 0; $i < 10000; $i++) {
$target = rand(333, 666);
foreach ($array as $current) {
if ($current === $target) break;
}
}
printf("%.5f\n", $begin + microtime(true));
1018$ php -d error_reporting=-1 trial.php
8.87178
0.33585
That's nearly nine seconds for the next/current version while not even half a second for the foreach version. Just don't.
It looks like each is basically a version of current() and next()
http://php.net/manual/en/function.current.php
http://php.net/manual/en/function.next.php
each() gives the current array item, and moves to the next index.
current() gives the current array item, but doen't increment the index.
So, you can replace each() with current(), and inside your foreach use next() to shift the index up
next() gives the next item, and increments the index.
while (list($key, $alreadyRP) = current($this->relatedProducts) && !$found) {
if ($alreadyRP->getProduct()->getId() === $rp->getProduct()->getId()) {
$found = true;
}
next($this->relatedProducts);
}
I have a very long list of strings called $stringfilter1 $stringfilter2 etc all the way up to $stringfilter50
I have another string $reporteremail and I want to make a conditional statement whereby if any of the $stringfilter strings is present in the $reporteremail, some code is executed. At the moment my code looks like this and it works:
if (stripos($reporteremail, $stringfilter1) !== false || stripos($reporteremail, $stringfilter2) !== false || stripos($reporteremail, $stringfilter3) !== false [...]) {
runcode();
}
This is very very long though. I have cut it short here.
I was wondering if there's a cleaner, more efficient way to do this?
EDIT:
I am writing a plugin for a bug tracker. The strings are entered on another page in text boxes. I access them on this page by running a function that looks like
$t_filter = plugin_config_get( 'filter1' );
$stringfilter1 = string_attribute( $t_filter1 );
I would agree looping through an array would be the best way to do this. How can I push each new string onto the end of an array without having to write that snippet above out 50 times?
How can I push each new string onto the end of an array without having to write that snippet above out 50 times?
Try this:
$needles = [];
for ($i = 0; $i < 50; $i++) {
$t_filter = plugin_config_get("filter$i");
$needles[] = string_attribute($t_filter);
}
I have a very long list of strings called $stringfilter1 $stringfilter2 etc all the way up to $stringfilter50
[...]
This is very very long though. I have cut it short here.
I was wondering if there's a cleaner, more efficient way to do this?
Try this, it should go after the code block above.
$flag = false;
foreach ($needles as $needle) {
if (stripos($reporteremail, $needle) !== false) {
$flag = true;
break;
}
}
if ($flag) {
runcode();
}
The code above works by iterating through the $needles array and sets a flag if stripos doesn't return false. After it's finished iterating, it checks if the flag is true, if so, this means that one of the needles was found in the array.
EDIT
Alternatively, you could do it all in one loop, which is both faster and more efficient.
$flag = false;
for ($i = 0; $i < 50; $i++) {
$t_filter = plugin_config_get("filter$i");
$needle = string_attribute($t_filter);
if (stripos($reporteremail, $needle) !== false) {
// One of the needles was found in $reporteremail.
runcode();
break;
}
}
You don't need a loop. First put all your filters in an array instead of having them in separate variables. I would try to do this by modifying the input source rather than doing it in your PHP script. (Based on your comments I'm not sure if that's possible or not, so maybe you do need a loop like the one in the other answer.) Then you can use str_ireplace to check for your filter strings in the $reporteremail. (This will not modify $reporteremail.)
str_ireplace($filters, '', $reporteremail, $count);
if ($count) {
// run code
}
The $count parameter will contain a count of how many replacements were performed. If it's nonzero, then at least one of the filters was found in $reporteremail.
I have found several reverse linked list implementation in php and most of them are the same with some little differences like this:
public function reverse() {
if ( $this->_firstNode !== NULL ) {
if ( $this->_firstNode->next !== NULL ) {
$reversed = $temp = NULL;
$current = $this->_firstNode;
while ( $current !== NULL ) {
$temp = $current->next;
$current->next = $reversed;
$reversed = $current;
$current = $temp;
}
$this->_firstNode = $reversed;
}
}
}
But I think it could be changed to this:
public function reverse() {
while ( $this->_firstNode->next !== NULL ) {
$oldFirstNode = $this->_firstNode;
$this->_firstNode = $oldFirstNode->next;
$oldFirstNode->next = NULL;
$this->_firstNode->next = $oldFirstNode;
}
}
Am I right?
Your code does not work, for two reasons:
You do not test for empty list. The method does not consider the case in which $this->_firstNode is NULL.
The method does not work if the list contains only one element.
If the list contains two or more elements, the method reverses only the first two elements of the list, then it falls in an endless loop. This is because in the last line of the body of the while you update $this->_firstNode->next with the value of $oldFirstNode, and in the next iteration you check for $this->_firstNode->next !== NULL, which is different from NULL since it is the value of $oldFirstNode, and the function continue looping on those two nodes.
For algorithm like this one, the best approach is to use paper and pencil and sketch the elements of the list and the variables pointing to them, and update them by following the algorithm step by step.
Finally note that if an algorithm is always is used for a certain basic task, it is very difficult to find a new, more efficient, algorithm.
I have a situation where I require to consult different objects in PHP via different methods to find some data.
The question regards more about formatting the code than an actual programming issue. What I am trying to do is not using several if's to gather this data like:
$data = obj->getData();
if (!isset($data)) $data = othObj->getThisData();
if (!isset($data)) $data = anothObj->getTheData();
if (!isset($data)) $data = anothOne->getAData();
...
process($data)
I was wondering what are the best practices in this case, if there is a better way using another procedures, like foreach or switch/case.
Thanks!
You can make an array of the possible objects you want to try, then run a loop. Might be more maintainable. This code can be modified to include parameters and use call_user_func_array instead.
$dataCallback = array(
array($othObj, 'getData'),
array($othObj, 'getThisData'),
array($anothObj, 'getTheData'),
array($anothOne, 'getAData'),
);
for($i = 0, $t = count($dataCallback); !isset($data) && $i < $t; $i++) {
$callback = $dataCallback[$i];
$data = call_user_func($callback);
}
if (isset($data))
process($data);
else
//no valid data returned at all ...
It doesn't look too bad the way it is.
It could be a bit more efficient if the if's were nested. e.g.
if (!isset($data = othObj->getData()))
if (!isset($data = othObj->getThisData()))
if (!isset($data = anothObj->getTheData()))
$data = anothOne->getAData()))
// ...
process($data)
Since there are less calls to isset (though they're pretty cheap anyway, so I wouldn't worry about it).
Personally I'd do something like this:
$data = null;
if (isset($obj->getData()) $data = $obj->getData();
else if (isset($othObj->getThisData()) $data = $othObj->getThisData();
else if (isset($anothObj->getTheData()) $data = $anothObj->getTheData();
else if (isset($anothOne->getAData()) $data = $anothOne->getAData();
process($data)
This saves processing time if the earlier objects actually return something. Since it is an elseif setup once it finds data it'll stop processing the other if clauses.
I don't think a switch statement would be appropriate in this case. Switches tend to be testing the value of one variable (is $a = 1, 2, 3 or 4).
($data = $ob1->get()) || ($data = $ob2->get()) || ($data = $ob3->get());
Would work, but if you're get function returns an empty array or false or empty string instead of NULL, it's going to continue looking for data...
I'd probably group the objects to ask for data into an array:
$objArray = array($obj, $othObj, $anothObj, ... );
Then run through a while loop until I had data:
$i = 0;
do {
$data = $objArray[$i]->getData();
$i++;
} while(!isset($data) && $i < count($objArray));