fread($file->getSize()); } /** * Gets the diff statistics such as inserted and deleted etc... * * @return array the statistics */ public static function getStatistics(): array { return Differ::getInstance()->getStatistics(); } /** * All-in-one static method to calculate the diff between two strings (or arrays of strings). * * @param string|string[] $old the old string (or array of lines) * @param string|string[] $new the new string (or array of lines) * @param string $renderer the renderer name * @param array $differOptions the options for Differ object * @param array $rendererOptions the options for renderer object * * @return string the rendered differences */ public static function calculate( $old, $new, string $renderer = 'Unified', array $differOptions = [], array $rendererOptions = [] ): string { // always convert into array form \is_string($old) && ($old = \explode("\n", $old)); \is_string($new) && ($new = \explode("\n", $new)); return RendererFactory::getInstance($renderer) ->setOptions($rendererOptions) ->render( Differ::getInstance() ->setOldNew($old, $new) ->setOptions($differOptions) ); } /** * All-in-one static method to calculate the diff between two files. * * @param string $old the path of the old file * @param string $new the path of the new file * @param string $renderer the renderer name * @param array $differOptions the options for Differ object * @param array $rendererOptions the options for renderer object * * @throws \LogicException path is a directory * @throws \RuntimeException path cannot be opened * * @return string the rendered differences */ public static function calculateFiles( string $old, string $new, string $renderer = 'Unified', array $differOptions = [], array $rendererOptions = [] ): string { // we want to leave the line-ending problem to static::calculate() // so do not set SplFileObject::DROP_NEW_LINE flag // otherwise, we will lose \r if the line-ending is \r\n $oldFile = new \SplFileObject($old, 'r'); $newFile = new \SplFileObject($new, 'r'); return static::calculate( // fread() requires the length > 0 hence we plus 1 for empty files $oldFile->fread($oldFile->getSize() + 1), $newFile->fread($newFile->getSize() + 1), $renderer, $differOptions, $rendererOptions ); } }