Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. Is this bad or am I just picky?

Is this bad or am I just picky?

Scheduled Pinned Locked Moved The Weird and The Wonderful
databasedata-structuresdebuggingquestioncode-review
19 Posts 15 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jeroen De Dauw
    wrote on last edited by
    #1

    Came across this "class" during a code review. My reaction is pretty much "the fuck is this". Do you think this is good code or not?

    class Listalizer {

    public static function listalize( $convert, &$data ) {
        if ( is\_object( $data ) ) {
            try {
                $data = call\_user\_func( $convert, $data );
            } catch ( \\Exception $ex ) {
                try {
                    trigger\_error( "listalize: Callable " . self::callable2String( $convert )
                        . " caused an exception: " . $ex->getMessage(), E\_USER\_WARNING );
                } catch ( \\Exception $exx ) {
                    // Argh! We \*can't\* throw an exception!
                    $exx = (object)$exx; // this is just a breakpoint anchor.
                }
    
                $data = false;
            }
        }
    
        if ( is\_array( $data ) || $data instanceof \\ArrayObject ) {
            foreach ( $data as $key => &$value ) {
                self::listalize( $convert, $value );
            }
        }
    }
    
    public static function objectify( $convert, &$data, $role = null ) {
        if ( is\_array( $data ) ) {
            try {
                $data = call\_user\_func( $convert, $data, $role );
            } catch ( \\Exception $ex ) {
                try {
                    trigger\_error( "objectify: Callable " . self::callable2String( $convert )
                        . " caused an exception: " . $ex->getMessage(), E\_USER\_WARNING );
                } catch ( \\Exception $exx ) {
                    // Argh! We \*can't\* throw an exception!
                    $exx = (object)$exx; // this is just a breakpoint anchor.
                }
    
                $data = false;
            }
        }
    
        if ( is\_array( $data ) || $data instanceof \\ArrayObject ) {
            foreach ( $data as $key => &$value ) {
                self::objectify( $convert, $value, $key );
            }
        }
    }
    
    public static function callable2String( $callable ) {
        if ( is\_array( $callable ) && count( $callable ) === 1 ) {
            $callable = array\_pop( $callable );
        }
    
        if ( is\_string( $callable ) ) {
            return $callable;
        } elseif ( is\_object( $callable ) ) {
            return get\_class( $callable ) . '->\_\_invoke';
        } elseif ( is\_array( $callable ) ) {
            $target = $callable\[0\];
    
    B _ I S L 10 Replies Last reply
    0
    • J Jeroen De Dauw

      Came across this "class" during a code review. My reaction is pretty much "the fuck is this". Do you think this is good code or not?

      class Listalizer {

      public static function listalize( $convert, &$data ) {
          if ( is\_object( $data ) ) {
              try {
                  $data = call\_user\_func( $convert, $data );
              } catch ( \\Exception $ex ) {
                  try {
                      trigger\_error( "listalize: Callable " . self::callable2String( $convert )
                          . " caused an exception: " . $ex->getMessage(), E\_USER\_WARNING );
                  } catch ( \\Exception $exx ) {
                      // Argh! We \*can't\* throw an exception!
                      $exx = (object)$exx; // this is just a breakpoint anchor.
                  }
      
                  $data = false;
              }
          }
      
          if ( is\_array( $data ) || $data instanceof \\ArrayObject ) {
              foreach ( $data as $key => &$value ) {
                  self::listalize( $convert, $value );
              }
          }
      }
      
      public static function objectify( $convert, &$data, $role = null ) {
          if ( is\_array( $data ) ) {
              try {
                  $data = call\_user\_func( $convert, $data, $role );
              } catch ( \\Exception $ex ) {
                  try {
                      trigger\_error( "objectify: Callable " . self::callable2String( $convert )
                          . " caused an exception: " . $ex->getMessage(), E\_USER\_WARNING );
                  } catch ( \\Exception $exx ) {
                      // Argh! We \*can't\* throw an exception!
                      $exx = (object)$exx; // this is just a breakpoint anchor.
                  }
      
                  $data = false;
              }
          }
      
          if ( is\_array( $data ) || $data instanceof \\ArrayObject ) {
              foreach ( $data as $key => &$value ) {
                  self::objectify( $convert, $value, $key );
              }
          }
      }
      
      public static function callable2String( $callable ) {
          if ( is\_array( $callable ) && count( $callable ) === 1 ) {
              $callable = array\_pop( $callable );
          }
      
          if ( is\_string( $callable ) ) {
              return $callable;
          } elseif ( is\_object( $callable ) ) {
              return get\_class( $callable ) . '->\_\_invoke';
          } elseif ( is\_array( $callable ) ) {
              $target = $callable\[0\];
      
      B Offline
      B Offline
      Brisingr Aerowing
      wrote on last edited by
      #2

      Ick. That's PHP, right?

      Bob Dole

      The internet is a great way to get on the net.

      :doh: 2.0.82.7292 SP6a

      J 1 Reply Last reply
      0
      • B Brisingr Aerowing

        Ick. That's PHP, right?

        Bob Dole

        The internet is a great way to get on the net.

        :doh: 2.0.82.7292 SP6a

        J Offline
        J Offline
        Jeroen De Dauw
        wrote on last edited by
        #3

        Yep, PHP. Though some people consider this to be a horror on itself, that's not what I am getting at here :)

        Jeroen De Dauw (blog | Twitter | Identi.ca)

        F G 2 Replies Last reply
        0
        • J Jeroen De Dauw

          Came across this "class" during a code review. My reaction is pretty much "the fuck is this". Do you think this is good code or not?

          class Listalizer {

          public static function listalize( $convert, &$data ) {
              if ( is\_object( $data ) ) {
                  try {
                      $data = call\_user\_func( $convert, $data );
                  } catch ( \\Exception $ex ) {
                      try {
                          trigger\_error( "listalize: Callable " . self::callable2String( $convert )
                              . " caused an exception: " . $ex->getMessage(), E\_USER\_WARNING );
                      } catch ( \\Exception $exx ) {
                          // Argh! We \*can't\* throw an exception!
                          $exx = (object)$exx; // this is just a breakpoint anchor.
                      }
          
                      $data = false;
                  }
              }
          
              if ( is\_array( $data ) || $data instanceof \\ArrayObject ) {
                  foreach ( $data as $key => &$value ) {
                      self::listalize( $convert, $value );
                  }
              }
          }
          
          public static function objectify( $convert, &$data, $role = null ) {
              if ( is\_array( $data ) ) {
                  try {
                      $data = call\_user\_func( $convert, $data, $role );
                  } catch ( \\Exception $ex ) {
                      try {
                          trigger\_error( "objectify: Callable " . self::callable2String( $convert )
                              . " caused an exception: " . $ex->getMessage(), E\_USER\_WARNING );
                      } catch ( \\Exception $exx ) {
                          // Argh! We \*can't\* throw an exception!
                          $exx = (object)$exx; // this is just a breakpoint anchor.
                      }
          
                      $data = false;
                  }
              }
          
              if ( is\_array( $data ) || $data instanceof \\ArrayObject ) {
                  foreach ( $data as $key => &$value ) {
                      self::objectify( $convert, $value, $key );
                  }
              }
          }
          
          public static function callable2String( $callable ) {
              if ( is\_array( $callable ) && count( $callable ) === 1 ) {
                  $callable = array\_pop( $callable );
              }
          
              if ( is\_string( $callable ) ) {
                  return $callable;
              } elseif ( is\_object( $callable ) ) {
                  return get\_class( $callable ) . '->\_\_invoke';
              } elseif ( is\_array( $callable ) ) {
                  $target = $callable\[0\];
          
          _ Offline
          _ Offline
          _beauw_
          wrote on last edited by
          #4

          It has the air of over-architected gibberish about it, but I don't even know what language that is. EDIT: OK, it's PHP. I'd be curious to hear what it enables that wouldn't otherwise be possible. I suspect the answer is "nothing."

          1 Reply Last reply
          0
          • J Jeroen De Dauw

            Yep, PHP. Though some people consider this to be a horror on itself, that's not what I am getting at here :)

            Jeroen De Dauw (blog | Twitter | Identi.ca)

            F Offline
            F Offline
            fjdiewornncalwe
            wrote on last edited by
            #5

            Could be worse. It could have been vb... :)

            I wasn't, now I am, then I won't be anymore.

            B 1 Reply Last reply
            0
            • J Jeroen De Dauw

              Came across this "class" during a code review. My reaction is pretty much "the fuck is this". Do you think this is good code or not?

              class Listalizer {

              public static function listalize( $convert, &$data ) {
                  if ( is\_object( $data ) ) {
                      try {
                          $data = call\_user\_func( $convert, $data );
                      } catch ( \\Exception $ex ) {
                          try {
                              trigger\_error( "listalize: Callable " . self::callable2String( $convert )
                                  . " caused an exception: " . $ex->getMessage(), E\_USER\_WARNING );
                          } catch ( \\Exception $exx ) {
                              // Argh! We \*can't\* throw an exception!
                              $exx = (object)$exx; // this is just a breakpoint anchor.
                          }
              
                          $data = false;
                      }
                  }
              
                  if ( is\_array( $data ) || $data instanceof \\ArrayObject ) {
                      foreach ( $data as $key => &$value ) {
                          self::listalize( $convert, $value );
                      }
                  }
              }
              
              public static function objectify( $convert, &$data, $role = null ) {
                  if ( is\_array( $data ) ) {
                      try {
                          $data = call\_user\_func( $convert, $data, $role );
                      } catch ( \\Exception $ex ) {
                          try {
                              trigger\_error( "objectify: Callable " . self::callable2String( $convert )
                                  . " caused an exception: " . $ex->getMessage(), E\_USER\_WARNING );
                          } catch ( \\Exception $exx ) {
                              // Argh! We \*can't\* throw an exception!
                              $exx = (object)$exx; // this is just a breakpoint anchor.
                          }
              
                          $data = false;
                      }
                  }
              
                  if ( is\_array( $data ) || $data instanceof \\ArrayObject ) {
                      foreach ( $data as $key => &$value ) {
                          self::objectify( $convert, $value, $key );
                      }
                  }
              }
              
              public static function callable2String( $callable ) {
                  if ( is\_array( $callable ) && count( $callable ) === 1 ) {
                      $callable = array\_pop( $callable );
                  }
              
                  if ( is\_string( $callable ) ) {
                      return $callable;
                  } elseif ( is\_object( $callable ) ) {
                      return get\_class( $callable ) . '->\_\_invoke';
                  } elseif ( is\_array( $callable ) ) {
                      $target = $callable\[0\];
              
              I Offline
              I Offline
              Ingo
              wrote on last edited by
              #6

              Jeroen De Dauw wrote:

              My reaction is pretty much "the f*** is this". Do you think this is good code or not?

              Well the try-blocks in the catch-statements are... Well, I don't know. It's not the best code, but I've seen much worse. I think with some arguments this programmer would be able to code readable, efficient code. So there is more than a shimmer of hope. ;)

              ------------------------------ Author of Primary ROleplaying SysTem How do I take my coffee? Black as midnight on a moonless night. War doesn't determine who's right. War determines who's left.

              1 Reply Last reply
              0
              • J Jeroen De Dauw

                Came across this "class" during a code review. My reaction is pretty much "the fuck is this". Do you think this is good code or not?

                class Listalizer {

                public static function listalize( $convert, &$data ) {
                    if ( is\_object( $data ) ) {
                        try {
                            $data = call\_user\_func( $convert, $data );
                        } catch ( \\Exception $ex ) {
                            try {
                                trigger\_error( "listalize: Callable " . self::callable2String( $convert )
                                    . " caused an exception: " . $ex->getMessage(), E\_USER\_WARNING );
                            } catch ( \\Exception $exx ) {
                                // Argh! We \*can't\* throw an exception!
                                $exx = (object)$exx; // this is just a breakpoint anchor.
                            }
                
                            $data = false;
                        }
                    }
                
                    if ( is\_array( $data ) || $data instanceof \\ArrayObject ) {
                        foreach ( $data as $key => &$value ) {
                            self::listalize( $convert, $value );
                        }
                    }
                }
                
                public static function objectify( $convert, &$data, $role = null ) {
                    if ( is\_array( $data ) ) {
                        try {
                            $data = call\_user\_func( $convert, $data, $role );
                        } catch ( \\Exception $ex ) {
                            try {
                                trigger\_error( "objectify: Callable " . self::callable2String( $convert )
                                    . " caused an exception: " . $ex->getMessage(), E\_USER\_WARNING );
                            } catch ( \\Exception $exx ) {
                                // Argh! We \*can't\* throw an exception!
                                $exx = (object)$exx; // this is just a breakpoint anchor.
                            }
                
                            $data = false;
                        }
                    }
                
                    if ( is\_array( $data ) || $data instanceof \\ArrayObject ) {
                        foreach ( $data as $key => &$value ) {
                            self::objectify( $convert, $value, $key );
                        }
                    }
                }
                
                public static function callable2String( $callable ) {
                    if ( is\_array( $callable ) && count( $callable ) === 1 ) {
                        $callable = array\_pop( $callable );
                    }
                
                    if ( is\_string( $callable ) ) {
                        return $callable;
                    } elseif ( is\_object( $callable ) ) {
                        return get\_class( $callable ) . '->\_\_invoke';
                    } elseif ( is\_array( $callable ) ) {
                        $target = $callable\[0\];
                
                S Offline
                S Offline
                StM0n
                wrote on last edited by
                #7

                Mhm... lot of ifs and elseifs... and try-catches. Could be more compact...

                (yes|no|maybe)*

                1 Reply Last reply
                0
                • J Jeroen De Dauw

                  Came across this "class" during a code review. My reaction is pretty much "the fuck is this". Do you think this is good code or not?

                  class Listalizer {

                  public static function listalize( $convert, &$data ) {
                      if ( is\_object( $data ) ) {
                          try {
                              $data = call\_user\_func( $convert, $data );
                          } catch ( \\Exception $ex ) {
                              try {
                                  trigger\_error( "listalize: Callable " . self::callable2String( $convert )
                                      . " caused an exception: " . $ex->getMessage(), E\_USER\_WARNING );
                              } catch ( \\Exception $exx ) {
                                  // Argh! We \*can't\* throw an exception!
                                  $exx = (object)$exx; // this is just a breakpoint anchor.
                              }
                  
                              $data = false;
                          }
                      }
                  
                      if ( is\_array( $data ) || $data instanceof \\ArrayObject ) {
                          foreach ( $data as $key => &$value ) {
                              self::listalize( $convert, $value );
                          }
                      }
                  }
                  
                  public static function objectify( $convert, &$data, $role = null ) {
                      if ( is\_array( $data ) ) {
                          try {
                              $data = call\_user\_func( $convert, $data, $role );
                          } catch ( \\Exception $ex ) {
                              try {
                                  trigger\_error( "objectify: Callable " . self::callable2String( $convert )
                                      . " caused an exception: " . $ex->getMessage(), E\_USER\_WARNING );
                              } catch ( \\Exception $exx ) {
                                  // Argh! We \*can't\* throw an exception!
                                  $exx = (object)$exx; // this is just a breakpoint anchor.
                              }
                  
                              $data = false;
                          }
                      }
                  
                      if ( is\_array( $data ) || $data instanceof \\ArrayObject ) {
                          foreach ( $data as $key => &$value ) {
                              self::objectify( $convert, $value, $key );
                          }
                      }
                  }
                  
                  public static function callable2String( $callable ) {
                      if ( is\_array( $callable ) && count( $callable ) === 1 ) {
                          $callable = array\_pop( $callable );
                      }
                  
                      if ( is\_string( $callable ) ) {
                          return $callable;
                      } elseif ( is\_object( $callable ) ) {
                          return get\_class( $callable ) . '->\_\_invoke';
                      } elseif ( is\_array( $callable ) ) {
                          $target = $callable\[0\];
                  
                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  Ok, I found a spelling mistake in the code (an extra 'x')

                  Jeroen De Dauw wrote:

                  $exx

                  1 Reply Last reply
                  0
                  • J Jeroen De Dauw

                    Came across this "class" during a code review. My reaction is pretty much "the fuck is this". Do you think this is good code or not?

                    class Listalizer {

                    public static function listalize( $convert, &$data ) {
                        if ( is\_object( $data ) ) {
                            try {
                                $data = call\_user\_func( $convert, $data );
                            } catch ( \\Exception $ex ) {
                                try {
                                    trigger\_error( "listalize: Callable " . self::callable2String( $convert )
                                        . " caused an exception: " . $ex->getMessage(), E\_USER\_WARNING );
                                } catch ( \\Exception $exx ) {
                                    // Argh! We \*can't\* throw an exception!
                                    $exx = (object)$exx; // this is just a breakpoint anchor.
                                }
                    
                                $data = false;
                            }
                        }
                    
                        if ( is\_array( $data ) || $data instanceof \\ArrayObject ) {
                            foreach ( $data as $key => &$value ) {
                                self::listalize( $convert, $value );
                            }
                        }
                    }
                    
                    public static function objectify( $convert, &$data, $role = null ) {
                        if ( is\_array( $data ) ) {
                            try {
                                $data = call\_user\_func( $convert, $data, $role );
                            } catch ( \\Exception $ex ) {
                                try {
                                    trigger\_error( "objectify: Callable " . self::callable2String( $convert )
                                        . " caused an exception: " . $ex->getMessage(), E\_USER\_WARNING );
                                } catch ( \\Exception $exx ) {
                                    // Argh! We \*can't\* throw an exception!
                                    $exx = (object)$exx; // this is just a breakpoint anchor.
                                }
                    
                                $data = false;
                            }
                        }
                    
                        if ( is\_array( $data ) || $data instanceof \\ArrayObject ) {
                            foreach ( $data as $key => &$value ) {
                                self::objectify( $convert, $value, $key );
                            }
                        }
                    }
                    
                    public static function callable2String( $callable ) {
                        if ( is\_array( $callable ) && count( $callable ) === 1 ) {
                            $callable = array\_pop( $callable );
                        }
                    
                        if ( is\_string( $callable ) ) {
                            return $callable;
                        } elseif ( is\_object( $callable ) ) {
                            return get\_class( $callable ) . '->\_\_invoke';
                        } elseif ( is\_array( $callable ) ) {
                            $target = $callable\[0\];
                    
                    J Offline
                    J Offline
                    jim lahey
                    wrote on last edited by
                    #9

                    Your reaction is the same as mine. Obvious digs at PHP aside, what does this class even do?

                    1 Reply Last reply
                    0
                    • J Jeroen De Dauw

                      Came across this "class" during a code review. My reaction is pretty much "the fuck is this". Do you think this is good code or not?

                      class Listalizer {

                      public static function listalize( $convert, &$data ) {
                          if ( is\_object( $data ) ) {
                              try {
                                  $data = call\_user\_func( $convert, $data );
                              } catch ( \\Exception $ex ) {
                                  try {
                                      trigger\_error( "listalize: Callable " . self::callable2String( $convert )
                                          . " caused an exception: " . $ex->getMessage(), E\_USER\_WARNING );
                                  } catch ( \\Exception $exx ) {
                                      // Argh! We \*can't\* throw an exception!
                                      $exx = (object)$exx; // this is just a breakpoint anchor.
                                  }
                      
                                  $data = false;
                              }
                          }
                      
                          if ( is\_array( $data ) || $data instanceof \\ArrayObject ) {
                              foreach ( $data as $key => &$value ) {
                                  self::listalize( $convert, $value );
                              }
                          }
                      }
                      
                      public static function objectify( $convert, &$data, $role = null ) {
                          if ( is\_array( $data ) ) {
                              try {
                                  $data = call\_user\_func( $convert, $data, $role );
                              } catch ( \\Exception $ex ) {
                                  try {
                                      trigger\_error( "objectify: Callable " . self::callable2String( $convert )
                                          . " caused an exception: " . $ex->getMessage(), E\_USER\_WARNING );
                                  } catch ( \\Exception $exx ) {
                                      // Argh! We \*can't\* throw an exception!
                                      $exx = (object)$exx; // this is just a breakpoint anchor.
                                  }
                      
                                  $data = false;
                              }
                          }
                      
                          if ( is\_array( $data ) || $data instanceof \\ArrayObject ) {
                              foreach ( $data as $key => &$value ) {
                                  self::objectify( $convert, $value, $key );
                              }
                          }
                      }
                      
                      public static function callable2String( $callable ) {
                          if ( is\_array( $callable ) && count( $callable ) === 1 ) {
                              $callable = array\_pop( $callable );
                          }
                      
                          if ( is\_string( $callable ) ) {
                              return $callable;
                          } elseif ( is\_object( $callable ) ) {
                              return get\_class( $callable ) . '->\_\_invoke';
                          } elseif ( is\_array( $callable ) ) {
                              $target = $callable\[0\];
                      
                      S Offline
                      S Offline
                      S Houghtelin
                      wrote on last edited by
                      #10

                      Too much

                      $exx

                      Or not enough...

                      It was broke, so I fixed it.

                      1 Reply Last reply
                      0
                      • J Jeroen De Dauw

                        Yep, PHP. Though some people consider this to be a horror on itself, that's not what I am getting at here :)

                        Jeroen De Dauw (blog | Twitter | Identi.ca)

                        G Offline
                        G Offline
                        Gary Huck
                        wrote on last edited by
                        #11

                        Okay ... what are you getting at?

                        1 Reply Last reply
                        0
                        • J Jeroen De Dauw

                          Came across this "class" during a code review. My reaction is pretty much "the fuck is this". Do you think this is good code or not?

                          class Listalizer {

                          public static function listalize( $convert, &$data ) {
                              if ( is\_object( $data ) ) {
                                  try {
                                      $data = call\_user\_func( $convert, $data );
                                  } catch ( \\Exception $ex ) {
                                      try {
                                          trigger\_error( "listalize: Callable " . self::callable2String( $convert )
                                              . " caused an exception: " . $ex->getMessage(), E\_USER\_WARNING );
                                      } catch ( \\Exception $exx ) {
                                          // Argh! We \*can't\* throw an exception!
                                          $exx = (object)$exx; // this is just a breakpoint anchor.
                                      }
                          
                                      $data = false;
                                  }
                              }
                          
                              if ( is\_array( $data ) || $data instanceof \\ArrayObject ) {
                                  foreach ( $data as $key => &$value ) {
                                      self::listalize( $convert, $value );
                                  }
                              }
                          }
                          
                          public static function objectify( $convert, &$data, $role = null ) {
                              if ( is\_array( $data ) ) {
                                  try {
                                      $data = call\_user\_func( $convert, $data, $role );
                                  } catch ( \\Exception $ex ) {
                                      try {
                                          trigger\_error( "objectify: Callable " . self::callable2String( $convert )
                                              . " caused an exception: " . $ex->getMessage(), E\_USER\_WARNING );
                                      } catch ( \\Exception $exx ) {
                                          // Argh! We \*can't\* throw an exception!
                                          $exx = (object)$exx; // this is just a breakpoint anchor.
                                      }
                          
                                      $data = false;
                                  }
                              }
                          
                              if ( is\_array( $data ) || $data instanceof \\ArrayObject ) {
                                  foreach ( $data as $key => &$value ) {
                                      self::objectify( $convert, $value, $key );
                                  }
                              }
                          }
                          
                          public static function callable2String( $callable ) {
                              if ( is\_array( $callable ) && count( $callable ) === 1 ) {
                                  $callable = array\_pop( $callable );
                              }
                          
                              if ( is\_string( $callable ) ) {
                                  return $callable;
                              } elseif ( is\_object( $callable ) ) {
                                  return get\_class( $callable ) . '->\_\_invoke';
                              } elseif ( is\_array( $callable ) ) {
                                  $target = $callable\[0\];
                          
                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #12

                          I think this just converts an array to a string. Can be used in those beginner/advanced/pro/guru code examples :)

                          1 Reply Last reply
                          0
                          • J Jeroen De Dauw

                            Came across this "class" during a code review. My reaction is pretty much "the fuck is this". Do you think this is good code or not?

                            class Listalizer {

                            public static function listalize( $convert, &$data ) {
                                if ( is\_object( $data ) ) {
                                    try {
                                        $data = call\_user\_func( $convert, $data );
                                    } catch ( \\Exception $ex ) {
                                        try {
                                            trigger\_error( "listalize: Callable " . self::callable2String( $convert )
                                                . " caused an exception: " . $ex->getMessage(), E\_USER\_WARNING );
                                        } catch ( \\Exception $exx ) {
                                            // Argh! We \*can't\* throw an exception!
                                            $exx = (object)$exx; // this is just a breakpoint anchor.
                                        }
                            
                                        $data = false;
                                    }
                                }
                            
                                if ( is\_array( $data ) || $data instanceof \\ArrayObject ) {
                                    foreach ( $data as $key => &$value ) {
                                        self::listalize( $convert, $value );
                                    }
                                }
                            }
                            
                            public static function objectify( $convert, &$data, $role = null ) {
                                if ( is\_array( $data ) ) {
                                    try {
                                        $data = call\_user\_func( $convert, $data, $role );
                                    } catch ( \\Exception $ex ) {
                                        try {
                                            trigger\_error( "objectify: Callable " . self::callable2String( $convert )
                                                . " caused an exception: " . $ex->getMessage(), E\_USER\_WARNING );
                                        } catch ( \\Exception $exx ) {
                                            // Argh! We \*can't\* throw an exception!
                                            $exx = (object)$exx; // this is just a breakpoint anchor.
                                        }
                            
                                        $data = false;
                                    }
                                }
                            
                                if ( is\_array( $data ) || $data instanceof \\ArrayObject ) {
                                    foreach ( $data as $key => &$value ) {
                                        self::objectify( $convert, $value, $key );
                                    }
                                }
                            }
                            
                            public static function callable2String( $callable ) {
                                if ( is\_array( $callable ) && count( $callable ) === 1 ) {
                                    $callable = array\_pop( $callable );
                                }
                            
                                if ( is\_string( $callable ) ) {
                                    return $callable;
                                } elseif ( is\_object( $callable ) ) {
                                    return get\_class( $callable ) . '->\_\_invoke';
                                } elseif ( is\_array( $callable ) ) {
                                    $target = $callable\[0\];
                            
                            S Offline
                            S Offline
                            Scott Burkow
                            wrote on last edited by
                            #13

                            the f***. developer needs more Argh comments. (am I supposed to include an emoticon?)

                            1 Reply Last reply
                            0
                            • J Jeroen De Dauw

                              Came across this "class" during a code review. My reaction is pretty much "the fuck is this". Do you think this is good code or not?

                              class Listalizer {

                              public static function listalize( $convert, &$data ) {
                                  if ( is\_object( $data ) ) {
                                      try {
                                          $data = call\_user\_func( $convert, $data );
                                      } catch ( \\Exception $ex ) {
                                          try {
                                              trigger\_error( "listalize: Callable " . self::callable2String( $convert )
                                                  . " caused an exception: " . $ex->getMessage(), E\_USER\_WARNING );
                                          } catch ( \\Exception $exx ) {
                                              // Argh! We \*can't\* throw an exception!
                                              $exx = (object)$exx; // this is just a breakpoint anchor.
                                          }
                              
                                          $data = false;
                                      }
                                  }
                              
                                  if ( is\_array( $data ) || $data instanceof \\ArrayObject ) {
                                      foreach ( $data as $key => &$value ) {
                                          self::listalize( $convert, $value );
                                      }
                                  }
                              }
                              
                              public static function objectify( $convert, &$data, $role = null ) {
                                  if ( is\_array( $data ) ) {
                                      try {
                                          $data = call\_user\_func( $convert, $data, $role );
                                      } catch ( \\Exception $ex ) {
                                          try {
                                              trigger\_error( "objectify: Callable " . self::callable2String( $convert )
                                                  . " caused an exception: " . $ex->getMessage(), E\_USER\_WARNING );
                                          } catch ( \\Exception $exx ) {
                                              // Argh! We \*can't\* throw an exception!
                                              $exx = (object)$exx; // this is just a breakpoint anchor.
                                          }
                              
                                          $data = false;
                                      }
                                  }
                              
                                  if ( is\_array( $data ) || $data instanceof \\ArrayObject ) {
                                      foreach ( $data as $key => &$value ) {
                                          self::objectify( $convert, $value, $key );
                                      }
                                  }
                              }
                              
                              public static function callable2String( $callable ) {
                                  if ( is\_array( $callable ) && count( $callable ) === 1 ) {
                                      $callable = array\_pop( $callable );
                                  }
                              
                                  if ( is\_string( $callable ) ) {
                                      return $callable;
                                  } elseif ( is\_object( $callable ) ) {
                                      return get\_class( $callable ) . '->\_\_invoke';
                                  } elseif ( is\_array( $callable ) ) {
                                      $target = $callable\[0\];
                              
                              S Offline
                              S Offline
                              satovey
                              wrote on last edited by
                              #14

                              After taking a good look see, this class takes an array, converts it to strings and tests to see if the string is a callable object with a method inside. In PHP, you can assign a function or object name to a string and call it as though you you had hard coded the name in the code. This code, despite all the complaints is actually what you would have to do in order to avoid the exceptions that occur when you call a function or object that does not exist. The author is utilizing a class perhaps to keep all the related code in one place, but a simple function would have sufficed.

                              1 Reply Last reply
                              0
                              • F fjdiewornncalwe

                                Could be worse. It could have been vb... :)

                                I wasn't, now I am, then I won't be anymore.

                                B Offline
                                B Offline
                                Bob G Beechey
                                wrote on last edited by
                                #15

                                I am getting tired of silly comments about vb. Presumably by someone who has not checked out vb since vb6.

                                F B H 3 Replies Last reply
                                0
                                • B Bob G Beechey

                                  I am getting tired of silly comments about vb. Presumably by someone who has not checked out vb since vb6.

                                  F Offline
                                  F Offline
                                  fjdiewornncalwe
                                  wrote on last edited by
                                  #16

                                  Apparently you missed the Joke icon. I've had to do plenty of vb and vb.net work over the years so I do not speak ignorantly on the matter. :)

                                  I wasn't, now I am, then I won't be anymore.

                                  B 1 Reply Last reply
                                  0
                                  • F fjdiewornncalwe

                                    Apparently you missed the Joke icon. I've had to do plenty of vb and vb.net work over the years so I do not speak ignorantly on the matter. :)

                                    I wasn't, now I am, then I won't be anymore.

                                    B Offline
                                    B Offline
                                    Bob G Beechey
                                    wrote on last edited by
                                    #17

                                    Damn. I knew I had to get my eyes tested.

                                    1 Reply Last reply
                                    0
                                    • B Bob G Beechey

                                      I am getting tired of silly comments about vb. Presumably by someone who has not checked out vb since vb6.

                                      B Offline
                                      B Offline
                                      Brady Kelly
                                      wrote on last edited by
                                      #18

                                      Last year I translated a whole lot of VB6 code into VB.NET 10. After the project was over he complained that most of my code was unintelligible and "I had used LINQ!"

                                      1 Reply Last reply
                                      0
                                      • B Bob G Beechey

                                        I am getting tired of silly comments about vb. Presumably by someone who has not checked out vb since vb6.

                                        H Offline
                                        H Offline
                                        H Brydon
                                        wrote on last edited by
                                        #19

                                        Bob G Beechey wrote:

                                        I am getting tired of silly comments about vb. Presumably by someone who has not checked out vb since vb6.

                                        I've used VB2 through VB6 and VB.Net (2002) through VB.Net 2005. VB's mother wears army boots!

                                        -- Harvey

                                        1 Reply Last reply
                                        0
                                        Reply
                                        • Reply as topic
                                        Log in to reply
                                        • Oldest to Newest
                                        • Newest to Oldest
                                        • Most Votes


                                        • Login

                                        • Don't have an account? Register

                                        • Login or register to search.
                                        • First post
                                          Last post
                                        0
                                        • Categories
                                        • Recent
                                        • Tags
                                        • Popular
                                        • World
                                        • Users
                                        • Groups