Magento – The use of function pathinfo() is discouraged Magento 2

coding-standardsfilefile uploadmagento-2.1magento2

I am checking uploaded file extension by pathinfo() function of php but in coding standard check it's shows

The use of function pathinfo() is discouraged

My code:

$fileExt = pathinfo($file, PATHINFO_EXTENSION);

Best Answer

You can use Magento\Framework\Filesystem\Io\File instead.

Example:

<?php

namespace Vendor\ModuleName\Model;

use Magento\Framework\Filesystem\Io\File;

class MyClass
{
   /**
    * @var File
    */
   private $file;

   /**
    * @param File $file
    */
   public function __construct(File $file)
   {
       $this->file = $file;
   }

   public function myMethod()
   {
       $path = 'this\is\some\dummy\path\potato.jpg';
       $pathInfo = $this->file->getPathInfo($path);
   }
}
Related Topic