PHPeas_Tests_PeaUtilsTests
Current view: /private/tmp/phpeas_17272677046aea870924f05.99267128/PHPeas/src/PHPeas/PeaUtils.php
Date: Mon Jul 30 22:11:54 CDT 2007 Executable lines: 35
Code covered: 100.00% Executed lines: 35
Legend: executed not executed dead code


       1                 : <?php                                                                                                                         
       2                 :                                                                                                                               
       3                 : /**                                                                                                                           
       4                 :  * began     : July 15, 2006                                                                                                  
       5                 :  * copyright : (c) 2006 - 2007, Russ Collier                                                                                  
       6                 :  * support   : phpeas@gmail.com                                                                                               
       7                 :  *                                                                                                                            
       8                 :  * This library is free software; you can redistribute it and/or                                                              
       9                 :  * modify it under the terms of the GNU Lesser General Public                                                                 
      10                 :  * License as published by the Free Software Foundation; version                                                              
      11                 :  * 2.1 of the License.                                                                                                        
      12                 :  *                                                                                                                            
      13                 :  * This library is distributed in the hope that it will be useful,                                                            
      14                 :  * but WITHOUT ANY WARRANTY; without even the implied warranty of                                                             
      15                 :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                                                          
      16                 :  * Lesser General Public License for more details.                                                                            
      17                 :  *                                                                                                                            
      18                 :  * You should have received a copy of the GNU Lesser General Public                                                           
      19                 :  * License along with this library; if not, write to the Free Software                                                        
      20                 :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA                                               
      21                 :  *                                                                                                                            
      22                 :  * Or visit http://www.gnu.org/licenses/lgpl.html                                                                             
      23                 :  */                                                                                                                           
      24                 :                                                                                                                               
      25                 : /**                                                                                                                           
      26                 :  * PHPeas_PeaUtils is a class that contains utility methods for performing                                                    
      27                 :  * various reflection and instrospection tasks on PHPeas.                                                                     
      28                 :  *                                                                                                                            
      29                 :  * @package PHPeas                                                                                                            
      30                 :  * @author  russ                                                                                                              
      31                 :  */                                                                                                                           
      32                 : class PHPeas_PeaUtils                                                                                                         
      33                 : {                                                                                                                             
      34                 :                                                                                                                               
      35                 :     const DESCRIBE_CLASS_KEY      = '!class';                                                                                 
      36                 :     const METHOD_PREFIX_READ      = 'get';                                                                                    
      37                 :     const METHOD_PREFIX_READ_BOOL = 'is';                                                                                     
      38                 :     const METHOD_PREFIX_WRITE     = 'set';                                                                                    
      39                 :                                                                                                                               
      40                 :     /**                                                                                                                       
      41                 :      * Clone a pea based on the available property getters and setters.                                                       
      42                 :      *                                                                                                                        
      43                 :      * <b>Note:</b> this method creates a <b>shallow</b> clone. In other words,                                               
      44                 :      * any objects referred to by the pea are shared with the clone rather than                                               
      45                 :      * being cloned in turn.                                                                                                  
      46                 :      *                                                                                                                        
      47                 :      * @param  Object $peaIn The pea to clone                                                                                 
      48                 :      * @return Object The shallow copy clone of the given pea                                                                 
      49                 :      * @throws ReflectionException                                                                                            
      50                 :      */                                                                                                                       
      51                 :     public static function clonePea( $peaIn )                                                                                 
      52                 :     {                                                                                                                         
      53                 :         if ( !( self::isValidPea( $peaIn ) ) )                                                                                
      54                 :         {                                                                                                                     
      55              10 :             throw new ReflectionException( 'Could not clone given pea since given pea is invalid' );                          
      56                 :         }                                                                                                                     
      57                 :                                                                                                                               
      58              10 :         $peaDescription = self::describe( $peaIn );                                                                           
      59                 :                                                                                                                               
      60              10 :         unset( $peaDescription[ self::DESCRIBE_CLASS_KEY ] );                                                                 
      61                 :                                                                                                                               
      62              10 :         $peaClassName = get_class( $peaIn );                                                                                  
      63                 :                                                                                                                               
      64              10 :         $clonePea = new $peaClassName();                                                                                      
      65                 :                                                                                                                               
      66              10 :         self::populate( $clonePea, $peaDescription );                                                                         
      67                 :                                                                                                                               
      68              10 :         return $clonePea;                                                                                                     
      69                 :     }                                                                                                                         
      70                 :                                                                                                                               
      71                 :     /**                                                                                                                       
      72                 :      * Copy property values from the origin bean to the destination pea for                                                   
      73                 :      * all cases where the property names are the same.                                                                       
      74                 :      *                                                                                                                        
      75                 :      * Properties that exist in the source pea, but do not exist in the                                                       
      76                 :      * destination pea (or are read-only in the destination pea) are silently                                                 
      77                 :      * ignored.                                                                                                               
      78                 :      *                                                                                                                        
      79                 :      * <b>Note:</b> this method performs a <b>shallow</b> copy. In other words,                                               
      80                 :      * any objects referred to by the source pea are shared with the                                                          
      81                 :      * destination pea rather than being cloned in turn.                                                                      
      82                 :      *                                                                                                                        
      83                 :      * @param  Object $srcPeaIn The pea to read properties from                                                               
      84                 :      * @param  Object $destPeaIn The pea to copy properties to                                                                
      85                 :      * @throws ReflectionException                                                                                            
      86                 :      */                                                                                                                       
      87                 :     public static function copyProperties( $srcPeaIn, $destPeaIn )                                                            
      88                 :     {                                                                                                                         
      89                 :         if ( !( self::isValidPea( $srcPeaIn ) ) )                                                                             
      90                 :         {                                                                                                                     
      91               9 :             throw new ReflectionException( 'Could not copy properties since given source pea is invalid' );                   
      92                 :         }                                                                                                                     
      93                 :                                                                                                                               
      94                 :         if ( !( self::isValidPea( $destPeaIn ) ) )                                                                            
      95                 :         {                                                                                                                     
      96               9 :             throw new ReflectionException( 'Could not copy properties since given destination pea is invalid' );              
      97                 :         }                                                                                                                     
      98                 :                                                                                                                               
      99               9 :         $srcPeaDescription = self::describe( $srcPeaIn );                                                                     
     100                 :                                                                                                                               
     101               9 :         unset( $srcPeaDescription[ self::DESCRIBE_CLASS_KEY ] );                                                              
     102                 :                                                                                                                               
     103               9 :         foreach ( $srcPeaDescription as $propertyName => $propertyValue )                                                     
     104                 :         {                                                                                                                     
     105                 :             $destWriteMethod = self::getWriteMethod(                                                                          
     106                 :                                                      $destPeaIn,                                                              
     107                 :                                                      $propertyName                                                            
     108                 :                                                    );                                                                         
     109                 :                                                                                                                               
     110                 :             if ( null != $destWriteMethod )                                                                                   
     111                 :             {                                                                                                                 
     112                 :                 $destPeaIn->$destWriteMethod( $propertyValue );                                                               
     113                 :             }                                                                                                                 
     114                 :         }                                                                                                                     
     115               9 :     }                                                                                                                         
     116                 :                                                                                                                               
     117                 :     /**                                                                                                                       
     118                 :      * Retuns an array with the property name as the index and the property                                                   
     119                 :      * value as the element value, for each property the given pea provides a                                                 
     120                 :      * read method for.                                                                                                       
     121                 :      *                                                                                                                        
     122                 :      * There is also an element in the return array with the name of the class                                                
     123                 :      * (the class name key is defined in the constant                                                                         
     124                 :      * PHPeas_PeaUtils::DESCRIBE_CLASS_KEY).                                                                                  
     125                 :      *                                                                                                                        
     126                 :      * <b>Note:</b> No order is guaranteed.                                                                                   
     127                 :      *                                                                                                                        
     128                 :      * @param  Object $peaIn The pea to describe                                                                              
     129                 :      * @return Mixed[] Property name/value pairs                                                                              
     130                 :      * @throws ReflectionException                                                                                            
     131                 :      */                                                                                                                       
     132                 :     public static function describe( $peaIn )                                                                                 
     133                 :     {                                                                                                                         
     134               1 :         if ( !( self::isValidPea( $peaIn ) ) )                                                                                
     135                 :         {                                                                                                                     
     136               8 :             throw new ReflectionException( 'Could not describe given pea since given pea is invalid' );                       
     137                 :         }                                                                                                                     
     138                 :                                                                                                                               
     139               1 :         $peaDesc = array();                                                                                                   
     140                 :                                                                                                                               
     141               1 :         $peaDesc[ self::DESCRIBE_CLASS_KEY ] = 'class ' . get_class( $peaIn );                                                
     142                 :                                                                                                                               
     143               1 :         $reflectedPea = new ReflectionClass( get_class( $peaIn ) );                                                           
     144                 :                                                                                                                               
     145               1 :         $readMethodPattern = '/^(' . self::METHOD_PREFIX_READ_BOOL . '|' .                                                    
     146                 :                                      self::METHOD_PREFIX_READ . ')/';                                                         
     147                 :                                                                                                                               
     148               1 :         foreach ( $reflectedPea->getMethods() as $reflectedMethod )                                                           
     149                 :         {                                                                                                                     
     150                 :             if ( $reflectedMethod->isPublic() &&                                                                              
     151                 :                  preg_match( $readMethodPattern, $reflectedMethod->getName() ) )                                              
     152                 :             {                                                                                                                 
     153                 :                 // Extract the property name from the method name                                                             
     154                 :                 $propertyName = $reflectedMethod->getName();                                                                  
     155                 :                                                                                                                               
     156                 :                 $propertyName = preg_replace( $readMethodPattern, '', $propertyName );                                        
     157                 :                                                                                                                               
     158                 :                 $firstLetter = $propertyName{ 0 };                                                                            
     159                 :                                                                                                                               
     160                 :                 $propertyName = strtolower( $firstLetter ) . substr( $propertyName, 1 );                                      
     161                 :                                                                                                                               
     162                 :                 $peaDesc[ $propertyName ] = $reflectedMethod->invoke( $peaIn, null );                                         
     163                 :             }                                                                                                                 
     164                 :         }                                                                                                                     
     165                 :                                                                                                                               
     166               1 :         $reflectedPea = null;                                                                                                 
     167                 :                                                                                                                               
     168               1 :         return $peaDesc;                                                                                                      
     169                 :     }                                                                                                                         
     170                 :                                                                                                                               
     171                 :     /*                                                                                                                        
     172                 :      * This method violates the PHPeas Spec's security rules                                                                  
     173                 :      * but I still think it's a cool little algorithm...                                                                      
     174                 :      *                                                                                                                        
     175                 :      * Gets an array of Strings representing all names of properties,                                                         
     176                 :      * regardless of visibility, for the given class and all of its ancestors.                                                
     177                 :      *                                                                                                                        
     178                 :      * <b>Note:</b> No order is guaranteed.                                                                                   
     179                 :      *                                                                                                                        
     180                 :      * @param  String $className The name of the class to find all the                                                        
     181                 :      *                           properties for                                                                               
     182                 :      * @return String[] An array of all property names                                                                        
     183                 :      *                                                                                                                        
     184                 :     public static function getAllClassProperties( $className )                                                                
     185                 :     {                                                                                                                         
     186                 :         $properties = array();                                                                                                
     187                 :                                                                                                                               
     188                 :         $reflectedClass = new ReflectionClass( $className );                                                                  
     189                 :                                                                                                                               
     190                 :          // If the given class has a parent class, then we need to call this                                                  
     191                 :          // method again and get the properties for that parent class, unless                                                 
     192                 :          // there is a grandparent class, in which case we recurse _again_, ad                                                
     193                 :          // infinitium.                                                                                                       
     194                 :         if ( $reflectedParent = $reflectedClass->getParentClass() )                                                           
     195                 :         {                                                                                                                     
     196                 :             $properties = array_merge(                                                                                        
     197                 :                                        $properties,                                                                           
     198                 :                                        self::getAllClassProperties( $reflectedParent->getName() )                             
     199                 :                                      );                                                                                       
     200                 :         }                                                                                                                     
     201                 :                                                                                                                               
     202                 :         foreach ( $reflectedClass->getProperties() as $reflectedProperty )                                                    
     203                 :         {                                                                                                                     
     204                 :             $properties[] = $reflectedProperty->getName();                                                                    
     205                 :         }                                                                                                                     
     206                 :                                                                                                                               
     207                 :         $reflectedParentClass = null;                                                                                         
     208                 :         $reflectedClass       = null;                                                                                         
     209                 :                                                                                                                               
     210                 :         return array_unique( $properties );                                                                                   
     211                 :     }*/                                                                                                                       
     212                 :                                                                                                                               
     213                 :     /**                                                                                                                       
     214                 :      * Gets the method that should be used to read the given property on the                                                  
     215                 :      * given pea.                                                                                                             
     216                 :      *                                                                                                                        
     217                 :      * @param  Object $peaIn The pea with the property in question                                                            
     218                 :      * @param  String $propertyNameIn The name of the property to find the                                                    
     219                 :      *                                read method for                                                                         
     220                 :      * @return String The name of the read method, or null if not found                                                       
     221                 :      * @throws ReflectionException                                                                                            
     222                 :      */                                                                                                                       
     223                 :     public static function getReadMethod( $peaIn, $propertyNameIn )                                                           
     224                 :     {                                                                                                                         
     225                 :         if ( !( self::isValidPea( $peaIn ) ) )                                                                                
     226                 :         {                                                                                                                     
     227                 :             throw new ReflectionException( 'No read methods can be found since given pea is invalid' );                       
     228                 :         }                                                                                                                     
     229                 :                                                                                                                               
     230                 :         if ( null == $propertyNameIn || '' == $propertyNameIn )                                                               
     231                 :         {                                                                                                                     
     232                 :             throw new ReflectionException( 'No read methods can be found since given property name is invalid' );             
     233                 :         }                                                                                                                     
     234                 :                                                                                                                               
     235                 :         $readMethod = null;                                                                                                   
     236                 :                                                                                                                               
     237                 :         $possibleReadMethods = array();                                                                                       
     238                 :                                                                                                                               
     239                 :         $possibleReadMethods[] = self::METHOD_PREFIX_READ_BOOL . ucfirst( $propertyNameIn );                                  
     240                 :         $possibleReadMethods[] = self::METHOD_PREFIX_READ      . ucfirst( $propertyNameIn );                                  
     241                 :                                                                                                                               
     242                 :         foreach ( $possibleReadMethods as $inferredMethodName )                                                               
     243                 :         {                                                                                                                     
     244                 :             if ( self::publicMethodExists( $peaIn, $inferredMethodName ) )                                                    
     245                 :             {                                                                                                                 
     246                 :                 $readMethod = $inferredMethodName;                                                                            
     247                 :                                                                                                                               
     248                 :                 break;                                                                                                        
     249                 :             }                                                                                                                 
     250                 :         }                                                                                                                     
     251                 :                                                                                                                               
     252                 :         return $readMethod;                                                                                                   
     253                 :     }                                                                                                                         
     254                 :                                                                                                                               
     255                 :     /**                                                                                                                       
     256                 :      * Gets the method that should be used to write to the given property on                                                  
     257                 :      * the given pea.                                                                                                         
     258                 :      *                                                                                                                        
     259                 :      * @param  Object $peaIn The pea with the property in question                                                            
     260                 :      * @param  String $propertyNameIn The name of the property to find the                                                    
     261                 :      *                                write method for                                                                        
     262                 :      * @return String The name of the write method, or null if not found                                                      
     263                 :      * @throws ReflectionException                                                                                            
     264                 :      */                                                                                                                       
     265                 :     public static function getWriteMethod( $peaIn, $propertyNameIn )                                                          
     266                 :     {                                                                                                                         
     267                 :         if ( !( self::isValidPea( $peaIn ) ) )                                                                                
     268                 :         {                                                                                                                     
     269                 :             throw new ReflectionException( 'No write methods can be found since given pea is invalid' );                      
     270                 :         }                                                                                                                     
     271                 :                                                                                                                               
     272                 :         if ( null == $propertyNameIn || '' == $propertyNameIn )                                                               
     273                 :         {                                                                                                                     
     274                 :             throw new ReflectionException( 'No write methods can be found since given property name is invalid' );            
     275                 :         }                                                                                                                     
     276                 :                                                                                                                               
     277                 :         $writeMethod = null;                                                                                                  
     278                 :                                                                                                                               
     279                 :         $inferredMethodName = self::METHOD_PREFIX_WRITE . ucfirst( $propertyNameIn );                                         
     280                 :                                                                                                                               
     281                 :         if ( self::publicMethodExists( $peaIn, $inferredMethodName ) )                                                        
     282                 :         {                                                                                                                     
     283                 :             $writeMethod = $inferredMethodName;                                                                               
     284                 :         }                                                                                                                     
     285                 :                                                                                                                               
     286                 :         return $writeMethod;                                                                                                  
     287                 :     }                                                                                                                         
     288                 :                                                                                                                               
     289                 :     /**                                                                                                                       
     290                 :      * Calls the given method name on the given pea with the given arguments.                                                 
     291                 :      *                                                                                                                        
     292                 :      * @param  Object $peaIn The pea to invoke the method on                                                                  
     293                 :      * @param  String $methodNameIn The name of the method to invoke                                                          
     294                 :      * @param  Mixed  $argsIn The list of method arguments                                                                    
     295                 :      * @return Mixed                                                                                                          
     296                 :      * @throws ReflectionException                                                                                            
     297                 :      */                                                                                                                       
     298                 :     public static function invokeMethod( $peaIn, $methodNameIn, $argsIn = null )                                              
     299                 :     {                                                                                                                         
     300                 :         if ( !( self::isValidPea( $peaIn ) ) )                                                                                
     301                 :         {                                                                                                                     
     302               5 :             throw new ReflectionException( 'Could not invoke method on given pea since given pea is invalid' );               
     303                 :         }                                                                                                                     
     304                 :                                                                                                                               
     305                 :         if ( null == $methodNameIn || '' == $methodNameIn )                                                                   
     306                 :         {                                                                                                                     
     307               5 :             throw new ReflectionException( 'Could not invoke method on given pea since given method name is invalid' );       
     308                 :         }                                                                                                                     
     309                 :                                                                                                                               
     310                 :         $reflectedPea = new ReflectionClass( get_class( $peaIn ) );                                                           
     311                 :                                                                                                                               
     312                 :         try                                                                                                                   
     313                 :         {                                                                                                                     
     314                 :             $reflectedMethod = $reflectedPea->getMethod( $methodNameIn );                                                     
     315                 :         }                                                                                                                     
     316                 :         catch ( ReflectionException $re )                                                                                     
     317                 :         {                                                                                                                     
     318               5 :             throw new ReflectionException( 'Could not invoke method since given method name does not exist in given pea' );   
     319                 :         }                                                                                                                     
     320                 :                                                                                                                               
     321                 :         return $reflectedMethod->invoke( $peaIn, $argsIn );                                                                   
     322                 :     }                                                                                                                         
     323                 :                                                                                                                               
     324                 :     /**                                                                                                                       
     325                 :      * Calls the given static method name from the given pea with the given                                                   
     326                 :      * arguments.                                                                                                             
     327                 :      *                                                                                                                        
     328                 :      * @param  Object $peaIn The pea to invoke the method from                                                                
     329                 :      * @param  String $methodNameIn The name of the static method to invoke                                                   
     330                 :      * @param  Mixed  $argsIn The list of method arguments                                                                    
     331                 :      * @return Mixed                                                                                                          
     332                 :      * @throws ReflectionException                                                                                            
     333                 :      */                                                                                                                       
     334                 :     public static function invokeStaticMethod( $peaIn, $methodNameIn, $argsIn = null )                                        
     335                 :     {                                                                                                                         
     336                 :                                                                                                                               
     337                 :         if ( !( self::isValidPea( $peaIn ) ) )                                                                                
     338                 :         {                                                                                                                     
     339               4 :             throw new ReflectionException( 'Could not invoke static method on given pea since given pea is invalid' );        
     340                 :         }                                                                                                                     
     341                 :                                                                                                                               
     342                 :         if ( null == $methodNameIn || '' == $methodNameIn )                                                                   
     343                 :         {                                                                                                                     
     344               4 :             throw new ReflectionException( 'Could not invoke static method on given pea since given method name is invalid' );
     345                 :         }                                                                                                                     
     346                 :                                                                                                                               
     347                 :         $reflectedPea = new ReflectionClass( get_class( $peaIn ) );                                                           
     348                 :                                                                                                                               
     349                 :         try                                                                                                                   
     350                 :         {                                                                                                                     
     351                 :             $reflectedMethod = $reflectedPea->getMethod( $methodNameIn );                                                     
     352                 :         }                                                                                                                     
     353                 :         catch ( ReflectionException $re )                                                                                     
     354                 :         {                                                                                                                     
     355               4 :             throw new ReflectionException( 'Could not invoke static method since given method name does ' .                   
     356                 :                                            'not exist in given pea' );                                                        
     357                 :         }                                                                                                                     
     358                 :                                                                                                                               
     359               4 :         if ( !( $reflectedMethod->isStatic() ) )                                                                              
     360                 :         {                                                                                                                     
     361               4 :             throw new ReflectionException( 'Could not invoke static method since given method is not actually static' );      
     362                 :         }                                                                                                                     
     363                 :                                                                                                                               
     364                 :         return $reflectedMethod->invoke( null, $argsIn );                                                                     
     365                 :     }                                                                                                                         
     366                 :                                                                                                                               
     367                 :     /**                                                                                                                       
     368                 :      * Populate the PHPeas properties of the specified pea, based on the                                                      
     369                 :      * specified name/value pairs.                                                                                            
     370                 :      *                                                                                                                        
     371                 :      * @param  Object $peaIn The pea to populate with the given property                                                      
     372                 :      *                       values                                                                                           
     373                 :      * @param  Mixed[] $propertiesIn Property name/value pairs                                                                
     374                 :      * @throws ReflectionException                                                                                            
     375                 :      */                                                                                                                       
     376                 :     public static function populate( $peaIn, $propertiesIn )                                                                  
     377                 :     {                                                                                                                         
     378               7 :         if ( !( self::isValidPea( $peaIn ) ) )                                                                                
     379                 :         {                                                                                                                     
     380               3 :             throw new ReflectionException( 'Could not populate given pea since given pea is invalid' );                       
     381                 :         }                                                                                                                     
     382                 :                                                                                                                               
     383               7 :         if ( null == $propertiesIn || !( is_array( $propertiesIn ) ) )                                                        
     384                 :         {                                                                                                                     
     385                 :             throw new ReflectionException( 'Could not populate given pea since given properties array is invalid' );          
     386                 :         }                                                                                                                     
     387                 :                                                                                                                               
     388               7 :         foreach ( $propertiesIn as $propertyName => $propertyValue )                                                          
     389                 :         {                                                                                                                     
     390                 :             $methodName = self::getWriteMethod( $peaIn, $propertyName );                                                      
     391                 :                                                                                                                               
     392                 :             if ( null != $methodName )                                                                                        
     393                 :             {                                                                                                                 
     394                 :                 $peaIn->$methodName( $propertyValue );                                                                        
     395                 :             }                                                                                                                 
     396                 :         }                                                                                                                     
     397               7 :     }                                                                                                                         
     398                 :                                                                                                                               
     399                 :     /**                                                                                                                       
     400                 :      * Returns true or false depending on whether or not the given pea is                                                     
     401                 :      * valid. Pea validity is defined as follows:                                                                             
     402                 :      * <br/>                                                                                                                  
     403                 :      * Not null<br />                                                                                                         
     404                 :      * Is an object<br />                                                                                                     
     405                 :      *                                                                                                                        
     406                 :      * @param  Object $peaIn The pea to test for validity                                                                     
     407                 :      * @return Boolean True if the given pea is valid, otherwise False                                                        
     408                 :      */                                                                                                                       
     409                 :     public static function isValidPea( $peaIn )                                                                               
     410                 :     {                                                                                                                         
     411                 :         $validPea = false;                                                                                                    
     412                 :                                                                                                                               
     413                 :         if ( null != $peaIn && is_object( $peaIn ) )                                                                          
     414                 :         {                                                                                                                     
     415                 :             $class = new ReflectionClass( get_class( $peaIn ) );                                                              
     416                 :                                                                                                                               
     417                 :             $constructor = $class->getConstructor();                                                                          
     418                 :                                                                                                                               
     419                 :             if ( ( null == $constructor ) ||                                                                                  
     420                 :                  (     $constructor->isConstructor() &&                                                                       
     421                 :                        $constructor->isPublic()      &&                                                                       
     422                 :                   0 == $constructor->getNumberOfRequiredParameters() ) )                                                      
     423                 :             {                                                                                                                 
     424                 :                 $validPea = true;                                                                                             
     425                 :             }                                                                                                                 
     426                 :         }                                                                                                                     
     427                 :                                                                                                                               
     428                 :         return $validPea;                                                                                                     
     429                 :     }                                                                                                                         
     430                 :                                                                                                                               
     431                 :     /**                                                                                                                       
     432                 :      * Returns true or false depending on whether or not the given method name                                                
     433                 :      * exists and is a public method on the given pea.                                                                        
     434                 :      *                                                                                                                        
     435                 :      * @param  Object $peaIn The pea to check the method on                                                                   
     436                 :      * @param  String $methodNameIn The method name to test                                                                   
     437                 :      * @return Boolean True if the given pea is valid, otherwise False                                                        
     438                 :      * @throws ReflectionException                                                                                            
     439                 :      */                                                                                                                       
     440                 :     public static function publicMethodExists( $peaIn, $methodNameIn )                                                        
     441                 :     {                                                                                                                         
     442                 :         $result = false;                                                                                                      
     443                 :                                                                                                                               
     444                 :         $reflectedClass = new ReflectionClass( get_class( $peaIn ) );                                                         
     445                 :                                                                                                                               
     446                 :         $reflectedMethod = null;                                                                                              
     447                 :                                                                                                                               
     448                 :         try                                                                                                                   
     449                 :         {                                                                                                                     
     450                 :             $reflectedMethod = $reflectedClass->getMethod( $methodNameIn );                                                   
     451                 :         }                                                                                                                     
     452                 :         catch ( ReflectionException $re )                                                                                     
     453                 :         {                                                                                                                     
     454                 :             $reflectedMethod = null;                                                                                          
     455                 :         }                                                                                                                     
     456                 :                                                                                                                               
     457                 :         if ( null != $reflectedMethod && $reflectedMethod->isPublic() )                                                       
     458                 :         {                                                                                                                     
     459                 :             $result = true;                                                                                                   
     460                 :         }                                                                                                                     
     461                 :                                                                                                                               
     462                 :         $reflectedMethod = null;                                                                                              
     463                 :         $reflectedClass  = null;                                                                                              
     464                 :                                                                                                                               
     465                 :         return $result;                                                                                                       
     466                 :     }                                                                                                                         
     467                 : }                                                                                                                             
     468                 :                                                                                                                               
     469                 : ?>                                                                                                                            

Generated by: PHPUnit 3.0.6 and Xdebug 1.3.2.