defmodule Phoenix.Endpoint.Server do
  # The supervisor for the underlying handlers.
  @moduledoc false

  @handler Phoenix.Endpoint.CowboyHandler
  use Supervisor

  def start_link(otp_app, endpoint) do
    if endpoint.config(:server) do
      Supervisor.start_link(__MODULE__, {otp_app, endpoint})
    else
      :ignore
    end
  end

  def init({otp_app, endpoint}) do
    import Supervisor.Spec

    children = []

    if config = endpoint.config(:http) do
      children =
        [@handler.child_spec(:http, endpoint, default(config, otp_app, 4000))|children]
    end

    if config = endpoint.config(:https) do
      {:ok, _} = Application.ensure_all_started(:ssl)
      children =
        [@handler.child_spec(:https, endpoint, default(config, otp_app, 4040))|children]
    end

    supervise(children, strategy: :one_for_one)
  end

  defp default(config, otp_app, port) do
    config =
      config
      |> Keyword.put_new(:otp_app, otp_app)
      |> Keyword.put_new(:port, port)

    if config[:port] == nil, do: raise ":port in config is nil. Use a valid port number."
    Keyword.put(config, :port, to_integer(config[:port]))
  end

  defp to_integer(binary)  when is_binary(binary),   do: String.to_integer(binary)
  defp to_integer(integer) when is_integer(integer), do: integer
end

defmodule Phoenix.Endpoint.Server do
  # The supervisor for the underlying handlers.
  @moduledoc false

  @handler Phoenix.Endpoint.CowboyHandler
  use Supervisor

  def start_link(otp_app, endpoint) do
    if endpoint.config(:server) do
      Supervisor.start_link(__MODULE__, {otp_app, endpoint})
    else
      :ignore
    end
  end

  def init({otp_app, endpoint}) do
    import Supervisor.Spec

    children = []

    if config = endpoint.config(:http) do
      children =
        [@handler.child_spec(:http, endpoint, default(config, otp_app, 4000))|children]
    end

    if config = endpoint.config(:https) do
      {:ok, _} = Application.ensure_all_started(:ssl)
      children =
        [@handler.child_spec(:https, endpoint, default(config, otp_app, 4040))|children]
    end

    supervise(children, strategy: :one_for_one)
  end

  defp default(config, otp_app, port) do
    config =
      config
      |> Keyword.put_new(:otp_app, otp_app)
      |> Keyword.put_new(:port, port)

    Keyword.put(config, :port, to_port(config[:port]))
  end

  defp to_port(nil), do: raise ":port in config is nil, please use a valid port number"
  defp to_port(binary)  when is_binary(binary),   do: String.to_integer(binary)
  defp to_port(integer) when is_integer(integer), do: integer
end

Move nil check to a to_port implementation
Move nil check to a to_port implementation

Joe-noh/phoenix,fbjork/phoenix,mschae/phoenix,hsavit1/phoenix,vic/phoenix,CultivateHQ/phoenix,gjaldon/phoenix,jamilabreu/phoenix,daytonn/phoenix,tony612/phoenix,jamilabreu/phoenix,scrogson/phoenix,manukall/phoenix,dmarkow/phoenix,greyhwndz/phoenix,DavidAlphaFox/phoenix,justnoxx/phoenix,manukall/phoenix,nshafer/phoenix,cas27/phoenix,wicliff/phoenix,sdnorm/phoenix,Dahoon/phoenix,yoavlt/phoenix,ericmj/phoenix,trarbr/phoenix,ybur-yug/phoenix,doomspork/phoenix,omotenko/phoenix_auth,bsodmike/phoenix,phoenixframework/phoenix,marcioj/phoenix,pap/phoenix,andrewvy/phoenix,safi/phoenix,CD1212/phoenix,Rumel/phoenix,switchflip/phoenix,Joe-noh/phoenix,mvidaurre/phoenix,Kosmas/phoenix,cas27/phoenix,y-yagi/phoenix,doron2402/phoenix,phoenixframework/phoenix,lono-devices/phoenix,jeffkreeftmeijer/phoenix,trarbr/phoenix,lono-devices/phoenix,wsmoak/phoenix,nshafer/phoenix,Eventacular/phoenix,KazuCocoa/phoenix,mobileoverlord/phoenix,Smulligan85/phoenix,ericmj/phoenix,jxs/phoenix,dmarkow/phoenix,JHKennedy4/phoenix,0x00evil/phoenix,CultivateHQ/phoenix,ACPK/phoenix,thegrubbsian/phoenix,rodrigues/phoenix,jeffweiss/phoenix,maxnordlund/phoenix,keathley/phoenix,sadiqmmm/phoenix,FND/phoenix,michalmuskala/phoenix,yonglehou/phoenix,andrewvy/phoenix,bitgamma/phoenix,jwarwick/phoenix,eksperimental/phoenix,SmartCasual/phoenix,jwarwick/phoenix,paulcsmith/phoenix,Gazler/phoenix,GabrielNicolasAvellaneda/phoenix,Kosmas/phoenix,brucify/phoenix,linearregression/phoenix,Rumel/phoenix,lancehalvorsen/phoenix,seejee/phoenix,VictorZeng/phoenix,nickgartmann/phoenix,javimolla/phoenix,lancehalvorsen/phoenix,mitchellhenke/phoenix,kholbekj/phoenix,stevedomin/phoenix,rafbgarcia/phoenix,optikfluffel/phoenix,kyledecot/phoenix,gjaldon/phoenix,pap/phoenix,kidaa/phoenix,robhurring/phoenix,evax/phoenix,beni55/phoenix,xtity/simple_phoenix,optikfluffel/phoenix,jasond-s/phoenix,stefania11/phoenix,begleynk/phoenix,0x00evil/phoenix,esbullington/phoenix-1,bsmr-erlang/phoenix,Smulligan85/phoenix,seejee/phoenix,robomc/phoenix,iStefo/phoenix,evax/phoenix,ahmetabdi/phoenix,phoenixframework/phoenix,korobkov/phoenix,optikfluffel/phoenix,elliotcm/phoenix,ephe-meral/phoenix,arthurcolle/phoenix,eksperimental/phoenix,stevedomin/phoenix,nshafer/phoenix,bsmr-erlang/phoenix,omotenko/phoenix_auth,iguberman/phoenix,keathley/phoenix,ugisozols/phoenix,Gazler/phoenix,rodrigues/phoenix,ybur-yug/phoenix,foxnewsnetwork/phoenix,rafbgarcia/phoenix,StevenNunez/phoenix,ephe-meral/phoenix,nicrioux/phoenix,korobkov/phoenix,wsmoak/phoenix,DavidAlphaFox/phoenix,bdtomlin/phoenix,mschae/phoenix,mobileoverlord/phoenix,vic/phoenix,rafbgarcia/phoenix,hotvulcan/phoenix,kevinold/phoenix,henrik/phoenix,michalmuskala/phoenix,milafrerichs/phoenix,Ben-Evolently/phoenix,Fluxuous/phoenix,romul/phoenix,0x00evil/phoenix,iStefo/phoenix,mvidaurre/phoenix,mitchellhenke/phoenix